org.wamblee.persistence
Class JpaMergeSupport

java.lang.Object
  extended by org.wamblee.persistence.JpaMergeSupport

public class JpaMergeSupport
extends Object

Support for merging of JPA entities. This utility allows the result of a merge (modifications of primary key and/or version) to be merged back into the argument that was merged. As a result, the merged entity can be reused and the application is not forced to use the new version that was returned from the merge. The utility traverses the object graph based on the public getter methods. Therefore, care should be taken with this utility as usage could lead to recursively loading all objects reachable from the given object. Then again, this utility is for working with detached objects and it would, in general, be bad practice to work with detached objects that still contain unresolved lazy loaded relations and with detached objects that implicitly refer to almost the entire datamodel. This utility best supports a service oriented design where interaction is through service interfaces where each service has its own storage isolated from other services. That would be opposed to a shared data design with many services acting on the same data.

Author:
Erik Brakkee

Constructor Summary
JpaMergeSupport()
          Constructs the object.
 
Method Summary
static void merge(Object aMerged, Object aPersistent)
          As #merge(Persistent) but with a given template.
static void processList(List aMerged, List aPersistent, List<ObjectElem> aProcessed)
          Process the persistent objects in the collections.
static
<Key,Value>
void
processMap(Map<Key,Value> aMerged, Map<Key,Value> aPersistent, List<ObjectElem> aProcessed)
          Process the Map objects in the collections.
static void processPersistent(Object aMerged, Object aPersistent, List<ObjectElem> aProcessed)
          Copies primary keys and version from the result of the merged to the object that was passed to the merge operation.
static void processSet(Set aMerged, Set aPersistent, List<ObjectElem> aProcessed)
          Process the persistent objects in sets.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JpaMergeSupport

public JpaMergeSupport()
Constructs the object.

Method Detail

merge

public static void merge(Object aMerged,
                         Object aPersistent)
As #merge(Persistent) but with a given template. This method can be accessed in a static way.

Parameters:
aMerge - The result of the call to EntityManager.merge(Object).
aPersistent - Object that was passed to EntityManager.merge(Object).

processPersistent

public static void processPersistent(Object aMerged,
                                     Object aPersistent,
                                     List<ObjectElem> aProcessed)
Copies primary keys and version from the result of the merged to the object that was passed to the merge operation. It does this by traversing the public properties of the object. It copies the primary key and version for objects that implement Persistent and applies the same rules to objects in maps and sets as well (i.e. recursively).

Parameters:
aPersistent - Object whose primary key and version are to be set.
aMerged - Object that was the result of the merge.
aProcessed - List of already processed Persistent objects of the persistent part.

processList

public static void processList(List aMerged,
                               List aPersistent,
                               List<ObjectElem> aProcessed)
Process the persistent objects in the collections.

Parameters:
aPersistent - Collection in the original object.
aMerged - Collection as a result of the merge.
aProcessed - List of processed persistent objects.

processSet

public static void processSet(Set aMerged,
                              Set aPersistent,
                              List<ObjectElem> aProcessed)
Process the persistent objects in sets.

Parameters:
aPersistent - Collection in the original object.
aMerged - Collection as a result of the merge.
aProcessed - List of processed persistent objects.

processMap

public static <Key,Value> void processMap(Map<Key,Value> aMerged,
                                          Map<Key,Value> aPersistent,
                                          List<ObjectElem> aProcessed)
Process the Map objects in the collections.

Parameters:
aPersistent - Collection in the original object.
aMerged - Collection as a result of the merge.
aProcessed - List of processed persistent objects.


Copyright © 2011. All Rights Reserved.