Raghu
Raghu

Reputation: 43

Mongo db entity relationship implementation using Spring data

I am learning Spring with Mongo DB and I'm feeling difficulty in learning the entity-relationship model. Can anyone teach me how can I implement the following design?

Person collection A person class

Sport collection A Sport class

while I am saving the person class which contains sports class, Sports entity should be saved in Sports collection if it is not already present and Person entity should be stored in Person collection with Sports objects Reference.

While I am retrieving Person class, associated sports class should be fetched from the corresponding collection. I have tried with @DBRef and it is not worked for me. It will be very helpful if anyone teaches me this scenario or giving the reference to learning this concept.

Very thanks in advance.

Upvotes: 0

Views: 319

Answers (1)

Nishant Bhardwaz
Nishant Bhardwaz

Reputation: 1001

while I am saving the person class which contains sports class, Sports entity should be saved in Sports collection if it is not already present and Person entity should be stored in Person collection with Sports objects Reference.

In Spring-data-mongo cascade save not supported. Therefore referencing object will not be saved to the database automatically. To achieve the same you have two option.
1) First, save sports collection (if that record not found in the collection) then save the reference of sports to person collection.
2) Make you custom cascade save implementation. For reference see this.

Upvotes: 0

Related Questions