Add merge method to return updated transient object for lists
authorCherian Mathew <c.mathew@bgbm.org>
Fri, 2 Oct 2015 09:10:18 +0000 (11:10 +0200)
committerCherian Mathew <c.mathew@bgbm.org>
Fri, 2 Oct 2015 09:10:18 +0000 (11:10 +0200)
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/IService.java
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/ServiceBase.java

index fc71f7ca4f9096797a508d475bfff029813cbcbd..367fe7a77d7412bb73372807854c8faf3e82703b 100644 (file)
@@ -19,10 +19,12 @@ import java.util.UUID;
 \r
 import org.hibernate.LockOptions;\r
 import org.hibernate.Session;\r
+import org.hibernate.event.spi.MergeEvent;\r
 \r
 import eu.etaxonomy.cdm.api.service.pager.Pager;\r
 import eu.etaxonomy.cdm.model.common.ICdmBase;\r
 import eu.etaxonomy.cdm.persistence.dao.initializer.IBeanInitializer;\r
+import eu.etaxonomy.cdm.persistence.hibernate.PostMergeEntityListener;\r
 import eu.etaxonomy.cdm.persistence.query.Grouping;\r
 import eu.etaxonomy.cdm.persistence.query.OrderHint;\r
 \r
@@ -350,6 +352,41 @@ public interface IService<T extends ICdmBase>{
     public List<T> merge(List<T> detachedObjects);\r
 \r
     /**\r
+     * This method allows for the possibility of returning the input transient\r
+     * entities instead of the merged persistent entity\r
+     *\r
+     * WARNING : This method should never be used when the objective of the merge\r
+     * is to attach to an existing session which is the standard use case.\r
+     * This method should only be used in the\r
+     * case of an external call which does not use hibernate sessions and is\r
+     * only interested in the entity as a POJO. Apart from the session information\r
+     * the only other difference between the transient and persisted object is in the case\r
+     * of new objects (id=0) where hibernate sets the id after commit. This id is copied\r
+     * over to the transient entity in {@link PostMergeEntityListener#onMerge(MergeEvent,Map)}\r
+     * making the two objects identical and allowing the transient object to be used further\r
+     * as a POJO\r
+     *\r
+     * @param detachedObjects\r
+     * @param returnTransientEntity\r
+     * @return\r
+     */\r
+    public List<T> merge(List<T> detachedObjects, boolean returnTransientEntity);\r
+\r
+    /**\r
+     * This method allows for the possibility of returning the input transient\r
+     * entity instead of the merged persistent entity\r
+     *\r
+     * WARNING : This method should never be used when the objective of the merge\r
+     * is to attach to an existing session which is the standard use case.\r
+     * This method should only be used in the\r
+     * case of an external call which does not use hibernate sessions and is\r
+     * only interested in the entity as a POJO. Apart from the session information\r
+     * the only other difference between the transient and persisted object is in the case\r
+     * of new objects (id=0) where hibernate sets the id after commit. This id is copied\r
+     * over to the transient entity in {@link PostMergeEntityListener#onMerge(MergeEvent,Map)}\r
+     * making the two objects identical and allowing the transient object to be used further\r
+     * as a POJO\r
+     *\r
      * @param newInstance\r
      * @param returnTransientEntity\r
      * @return\r
@@ -358,4 +395,6 @@ public interface IService<T extends ICdmBase>{
 \r
 \r
 \r
+\r
+\r
 }
\ No newline at end of file
index ad2f0aeb85ac056930e0ae871573fc51304ba08f..c4dee5e324f6f982c8feae3cee63cc0aece70552 100644 (file)
@@ -182,9 +182,15 @@ public abstract class ServiceBase<T extends CdmBase, DAO extends ICdmEntityDao<T
     @Override\r
     @Transactional(readOnly = false)\r
     public List<T> merge(List<T> detachedObjects) {\r
+        return merge(detachedObjects, false);\r
+    }\r
+\r
+    @Override\r
+    @Transactional(readOnly = false)\r
+    public List<T> merge(List<T> detachedObjects, boolean returnTransientEntity) {\r
         List<T> mergedObjects = new ArrayList<T>();\r
         for(T obj : detachedObjects) {\r
-            mergedObjects.add(dao.merge(obj));\r
+            mergedObjects.add(dao.merge(obj, returnTransientEntity));\r
         }\r
         return mergedObjects;\r
     }\r