@marianomdq: And your Spring data code should look something like this: ...
When I tried this, there is no problem that it never would be to save objects in the database. My problem loads them again. This, apparently, leads to the fact that the loading of the category (in this example) falls into an infinite loop. Category → Product → Category → Produ ..... Until a StackOverflowException is thrown.
java.lang.StackOverflowError: null at java.lang.reflect.Constructor.newInstance(Constructor.java:416) ~[na:1.8.0_77] at java.lang.Class.newInstance(Class.java:442) ~[na:1.8.0_77] at com.mongodb.DBCollectionObjectFactory.getInstance(DBCollectionObjectFactory.java:51) ~[mongodb-driver-3.2.2.jar:na] at com.mongodb.DBObjectCodec.readDocument(DBObjectCodec.java:340) ~[mongodb-driver-3.2.2.jar:na] at com.mongodb.DBObjectCodec.decode(DBObjectCodec.java:136) ~[mongodb-driver-3.2.2.jar:na] at com.mongodb.DBObjectCodec.decode(DBObjectCodec.java:61) ~[mongodb-driver-3.2.2.jar:na] ........
The full stacktrace is very large.
Solution : lazy loading database links!
Just set the lazy DBRef annotation flag to true, this will make the download links lazy. This means that circular links will not load immediately.
@DBRef(lazy = true)
The research that really came to this decision was cumbersome. So I wanted to share this information.
source share