[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/,如需转载,请注明出处,否则将追究法律责任。