6

I am new to MongoDB. I am preparing an example for learning. It looks like when working with bi-directional relationship with Spring Data & DBRef, it gets into end-less cycle and keeps reading the association again and again.

The domain class looks like

class Category{
    private String name;
    private Category parentCategory;
    private Set<Category> childCategories;
    // getter & setters 
}

I wonder if it is the rare scenario to have this sort of bi-directional relationship(quite commong in ORMs though), couldn't get any help on this. One option could be writing custom converter and ignoring anyone side of relation, but that will turn my in-memory object into an inconsistent state.

Please share ideas on this or some best practices on handling such scenarios.

Thanks, Vishal Shukla

Knut Herrmann
  • 30,880
  • 4
  • 31
  • 67
Vishal Shukla
  • 2,848
  • 2
  • 17
  • 19
  • 4
    did you solve this problem? If yes could you please let me know how did you solve the problem. – Debopam Apr 27 '15 at 11:18
  • I don't even think writing a custom converter would help. The `MongoMappingConverter` in spring-data tries to resolve all DBRefs and build out the `DBObject` of the resource you are trying to read before passing it to any custom converters: https://github.com/spring-projects/spring-data-mongodb/blob/master/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java#L895 So if your Mongo DB itself has DBRefs, spring-data will die before it reaches your custom converter. – heez Jun 28 '17 at 18:03

0 Answers0