Spring Data MongoDB cascade save on DBRef objects

jieforest發表於2012-05-28
[i=s] 本帖最後由 jieforest 於 2012-5-25 23:23 編輯

Spring Data MongoDB by default does not support cascade operations on referenced objects with @DBRef annotations as reference says:

The mapping framework does not handle cascading saves. If you change an Account object that is referenced by a Person object, you must savethe Account object separately. Calling save on the Person object will not automatically save the Account objects in the property accounts.

That’s quite problematic because in order to achieve saving child objects you need to override save method in repository in parent or create additional “service” methods like it is presented in here.

In this article I will show you how it can be achieved for all documents using generic implementation of AbstractMongoEventListener.

@CascadeSave annotation

Because we can’t change @DBRef annotation by adding cascade property lets create new annotation @CascadeSave that will be used to mark which fields should be saved when parent object is saved.

CODE:

package pl.maciejwalkowiak.springdata.mongodb;

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/301743/viewspace-731256/,如需轉載,請註明出處,否則將追究法律責任。

相關文章