Spring Data MongoDB cascade save on DBRef objects
[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.
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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- spring4.2+mongodb2.6+spring-data-mongodb1.8.2 整合SpringMongoDBDB2
- spring-data-mongodb常用操作SpringMongoDB
- Spring Data MongoDB支援GridFSSpringMongoDB
- PDO--PHP Data ObjectsPHPObject
- SpringBoot 整合 Spring Data Mongodb 操作 MongoDB 詳解Spring BootMongoDB
- spring data mongodb配置+月庫實現SpringMongoDB
- 2.1.3.3 Container Data Objects in a CDBAIObject
- Convert Array of Objects to Data TableObject
- rdo(remote data objects) repo openstack icehouseREMObject
- Spring認證中國教育管理中心-Spring Data MongoDB教程SpringMongoDB
- Spring Data MongoDB 操作 document DB 的詳細示例SpringMongoDB
- Introduce to Data Storage Objects in ASP.NETObjectASP.NET
- Using Morphia to map Java objects in MongoDBJavaObjectMongoDB
- 在Spring Data MongoDB中實現關係建模 - spring.ioSpringMongoDB
- spring-data-mongodb多資料庫訪問實現SpringMongoDB資料庫
- Spring-Data-Mongodb資料庫連線程式碼示例SpringMongoDB資料庫線程
- 2.3.2.3 Data-Linked Application Common ObjectsAPPObject
- object_id and data_object_id in dba_objectsObject
- MONGODB03 - 分組計數/分組去重計數(基於 spring-data-mongodb)MongoDBSpring
- Import Data From MS Excel to DataSet without using COM ObjectsImportExcelObject
- spring data mongodb 如何以事物的方式進行增刪改查SpringMongoDB
- 如何在Spring Data MongoDB 中儲存和查詢動態欄位SpringMongoDB
- Spring Data JpaSpring
- Spring Data JPA之Spring Data JPA快速入門(三)Spring
- PHP設計模式-DAO (Data Access Objects) 資料訪問物件模式PHP設計模式Object物件
- dubbo2.5-spring4-mybastis3.2-springmvc4-mongodb3.4-redis3.2整合(五)Spring中spring-data-redis的使用ASTS3SpringMVCMongoDBRedis
- Spring Boot:整合Spring Data JPASpring Boot
- npm install -save 和 -save-devNPMdev
- npm –save-dev –save 的區別NPMdev
- Spring Data 教程 - RedisSpringRedis
- Spring Data Jpa APISpringAPI
- spring 於mongodb整合SpringMongoDB
- ObjectsObject
- canvas save()Canvas
- 用 MongoTemplate 操作 documentDB 與 用 Spring Data MongoDB 操作 documentDB 有什麼不同,write concern,transactionSpringMongoDB
- Cascade屬性
- ORACLE CASCADE DATAGUARDOracle
- Spring Data JPA 的使用Spring