Spring Data MongoDB: how to implement "entity relationships"? Spring Data MongoDB: how to implement "entity relationships"? spring spring

Spring Data MongoDB: how to implement "entity relationships"?


You can use the @DBRef annotation to persist the referenced class in a separate collection, else the document will be persisted in the same document (json).The use of DBRef require an extra query for the mongodb driver, you should consider this to analyze performance issues.

From spring data documentation

@DBRef - applied at the field to indicate it is to be stored using a com.mongodb.DBRef.

7.3.4 Using DBRefsThe mapping framework doesn't have to store child objects embedded within the document. You can also store them separately and use a DBRef to refer to that document. When the object is loaded from MongoDB, those references will be eagerly resolved and you will get back a mapped object that looks the same as if it had been stored embedded within your master document.


You can use RelMongo framework which allows to implement relations by providing @OneToMany and @OneToOne annotations support.