Cannot create a reference to an object with a NULL id mongo hibernate-mongo and spring boot
This is happening becuase hibernate is not able to get id as it is not yet created so you need to save Device to DB first then set it into user entity.
Something like below.
B b = new B();
mongoOperations.save(b);
A a = new A();
a.setB(b)
mongoOperations.save(a);
The mapping framework does not handle cascading saves. If you change an Account object that is referenced by a Person object, you must save the Account object separately. Calling save on the Person object does not automatically save the Account objects in the accounts property.
Read below document please;
https://docs.spring.io/spring-data/mongodb/docs/2.1.4.RELEASE/reference/html/#mapping-usage-references