X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;ds=inline;f=support%2Fspring%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fpersistence%2Fhibernate%2FHibernateSupport.java;h=52dbc352b742a8ea2c0552b4b454dccc2b164bfd;hb=96c8961955a306314dfe0cf9ca192252de39fc1c;hp=8425facdf6d3c83ebfcf4d870c8eccf0e75a7b60;hpb=a503959a4290a415cfe6f668a301e44b4dcde445;p=utils
diff --git a/support/spring/src/main/java/org/wamblee/persistence/hibernate/HibernateSupport.java b/support/spring/src/main/java/org/wamblee/persistence/hibernate/HibernateSupport.java
index 8425facd..52dbc352 100644
--- a/support/spring/src/main/java/org/wamblee/persistence/hibernate/HibernateSupport.java
+++ b/support/spring/src/main/java/org/wamblee/persistence/hibernate/HibernateSupport.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2005 the original author or authors.
+ * Copyright 2005-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-
package org.wamblee.persistence.hibernate;
import java.lang.reflect.InvocationTargetException;
@@ -22,6 +21,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
+import java.util.Map.Entry;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -32,37 +32,12 @@ import org.wamblee.persistence.Persistent;
/**
* Extension of
* {@link org.springframework.orm.hibernate.support.HibernateDaoSupport}.
- *
+ *
* @author Erik Brakkee
*/
public class HibernateSupport extends HibernateDaoSupport {
-
private static final Log LOG = LogFactory.getLog(HibernateSupport.class);
- /**
- * This class provided an equality operation based on the object reference
- * of the wrapped object. This is required because we cannto assume that the
- * equals operation has any meaning for different types of persistent
- * objects. This allows us to use the standard collection classes for
- * detecting cyclic dependences and avoiding recursion.
- *
- */
- private static final class ObjectElem {
- private Object _object;
-
- public ObjectElem(Object aObject) {
- _object = aObject;
- }
-
- public boolean equals(Object aObj) {
- return ((ObjectElem) aObj)._object == _object;
- }
-
- public int hashCode() {
- return _object.hashCode();
- }
- }
-
/**
* Constructs the object.
*
@@ -72,14 +47,13 @@ public class HibernateSupport extends HibernateDaoSupport {
}
/**
- * Performes a hibernate Session.merge()
and updates the
- * object with the correct primary key and version. This is an extension to
- * the hibernate merge operation because hibernate itself leaves the object
- * passed to merge untouched.
- *
- * Use this method with extreme caution since it will recursively load all
- * objects that the current object has relations with and for which
- * cascade="merge" was specified in the Hibernate mapping file.
+ * Performes a hibernate Session.merge()
and updates the object
+ * with the correct primary key and version. This is an extension to the
+ * hibernate merge operation because hibernate itself leaves the object
+ * passed to merge untouched. Use this method with extreme caution since it
+ * will recursively load all objects that the current object has relations
+ * with and for which cascade="merge" was specified in the Hibernate mapping
+ * file.
*
* @param aPersistent
* Object to merge.
@@ -116,34 +90,40 @@ public class HibernateSupport extends HibernateDaoSupport {
* @param aProcessed
* List of already processed Persistent objects of the persistent
* part.
+ *
*/
public static void processPersistent(Persistent aPersistent,
- Persistent aMerged, List aProcessed) {
- if (aPersistent == null && aMerged == null) {
+ Persistent aMerged, List aProcessed) {
+ if ((aPersistent == null) && (aMerged == null)) {
return;
}
- if (aPersistent == null || aMerged == null) {
- throw new RuntimeException("persistent or merged object is null '"
- + aPersistent + "'" + " '" + aMerged + "'");
+
+ if ((aPersistent == null) || (aMerged == null)) {
+ throw new RuntimeException("persistent or merged object is null '" +
+ aPersistent + "'" + " '" + aMerged + "'");
}
+
ObjectElem elem = new ObjectElem(aPersistent);
+
if (aProcessed.contains(elem)) {
return; // already processed.
}
+
aProcessed.add(elem);
LOG.debug("Setting pk/version on " + aPersistent + " from " + aMerged);
- if (aPersistent.getPrimaryKey() != null
- && !aMerged.getPrimaryKey().equals(aPersistent.getPrimaryKey())) {
- LOG.error("Mismatch between primary key values: " + aPersistent
- + " " + aMerged);
+ if ((aPersistent.getPrimaryKey() != null) &&
+ !aMerged.getPrimaryKey().equals(aPersistent.getPrimaryKey())) {
+ LOG.error("Mismatch between primary key values: " + aPersistent +
+ " " + aMerged);
} else {
aPersistent.setPersistedVersion(aMerged.getPersistedVersion());
aPersistent.setPrimaryKey(aMerged.getPrimaryKey());
}
Method[] methods = aPersistent.getClass().getMethods();
+
for (Method getter : methods) {
if (getter.getName().startsWith("get")) {
Class returnType = getter.getReturnType();
@@ -164,18 +144,19 @@ public class HibernateSupport extends HibernateDaoSupport {
} else if (Persistent.class.isAssignableFrom(returnType)) {
Persistent merged = (Persistent) getter.invoke(aMerged);
Persistent persistent = (Persistent) getter
- .invoke(aPersistent);
+ .invoke(aPersistent);
processPersistent(persistent, merged, aProcessed);
- } else if (returnType.isArray()
- && Persistent.class.isAssignableFrom(returnType
- .getComponentType())) {
+ } else if (returnType.isArray() &&
+ Persistent.class.isAssignableFrom(returnType
+ .getComponentType())) {
Persistent[] merged = (Persistent[]) getter
- .invoke(aMerged);
+ .invoke(aMerged);
Persistent[] persistent = (Persistent[]) getter
- .invoke(aPersistent);
+ .invoke(aPersistent);
+
for (int i = 0; i < persistent.length; i++) {
processPersistent(persistent[i], merged[i],
- aProcessed);
+ aProcessed);
}
}
} catch (InvocationTargetException e) {
@@ -185,7 +166,6 @@ public class HibernateSupport extends HibernateDaoSupport {
}
}
}
-
}
/**
@@ -197,20 +177,24 @@ public class HibernateSupport extends HibernateDaoSupport {
* Collection as a result of the merge.
* @param aProcessed
* List of processed persistent objects.
+ *
*/
public static void processList(List aPersistent, List aMerged,
- List aProcessed) {
+ List aProcessed) {
Object[] merged = aMerged.toArray();
Object[] persistent = aPersistent.toArray();
+
if (merged.length != persistent.length) {
- throw new RuntimeException("Array sizes differ " + merged.length
- + " " + persistent.length);
+ throw new RuntimeException("Array sizes differ " + merged.length +
+ " " + persistent.length);
}
+
for (int i = 0; i < merged.length; i++) {
assert merged[i].equals(persistent[i]);
+
if (merged[i] instanceof Persistent) {
processPersistent((Persistent) persistent[i],
- (Persistent) merged[i], aProcessed);
+ (Persistent) merged[i], aProcessed);
}
}
}
@@ -224,19 +208,22 @@ public class HibernateSupport extends HibernateDaoSupport {
* Collection as a result of the merge.
* @param aProcessed
* List of processed persistent objects.
+ *
*/
public static void processSet(Set aPersistent, Set aMerged,
- List aProcessed) {
+ List aProcessed) {
if (aMerged.size() != aPersistent.size()) {
- throw new RuntimeException("Array sizes differ " + aMerged.size()
- + " " + aPersistent.size());
+ throw new RuntimeException("Array sizes differ " + aMerged.size() +
+ " " + aPersistent.size());
}
+
for (Object merged : aMerged) {
// Find the object that equals the merged[i]
for (Object persistent : aPersistent) {
if (persistent.equals(merged)) {
processPersistent((Persistent) persistent,
- (Persistent) merged, aProcessed);
+ (Persistent) merged, aProcessed);
+
break;
}
}
@@ -252,30 +239,64 @@ public class HibernateSupport extends HibernateDaoSupport {
* Collection as a result of the merge.
* @param aProcessed
* List of processed persistent objects.
+ *
*/
- public static void processMap(Map aPersistent, Map aMerged,
- List aProcessed) {
+ public static void processMap(Map aPersistent, Map aMerged,
+ List aProcessed) {
if (aMerged.size() != aPersistent.size()) {
- throw new RuntimeException("Sizes differ " + aMerged.size() + " "
- + aPersistent.size());
+ throw new RuntimeException("Sizes differ " + aMerged.size() + " " +
+ aPersistent.size());
}
- Set keys = aMerged.keySet();
- for (Object key : keys) {
+
+ Set> entries = aMerged.entrySet();
+
+ for (Entry entry : entries) {
+ Key key = entry.getKey();
if (!aPersistent.containsKey(key)) {
throw new RuntimeException("Key '" + key + "' not found");
}
- Object mergedValue = aMerged.get(key);
+
+ Value mergedValue = entry.getValue();
Object persistentValue = aPersistent.get(key);
+
if (mergedValue instanceof Persistent) {
if (persistentValue instanceof Persistent) {
processPersistent((Persistent) persistentValue,
- (Persistent) mergedValue, aProcessed);
+ (Persistent) mergedValue, aProcessed);
} else {
throw new RuntimeException(
- "Value in original object is null, whereas merged object contains a value");
+ "Value in original object is null, whereas merged object contains a value");
}
}
}
}
+ /**
+ * This class provided an equality operation based on the object reference
+ * of the wrapped object. This is required because we cannto assume that the
+ * equals operation has any meaning for different types of persistent
+ * objects. This allows us to use the standard collection classes for
+ * detecting cyclic dependences and avoiding recursion.
+ */
+ private static final class ObjectElem {
+ private Object object;
+
+ public ObjectElem(Object aObject) {
+ object = aObject;
+ }
+
+ public boolean equals(Object aObj) {
+ if (aObj == null) {
+ return false;
+ }
+ if (!(aObj instanceof ObjectElem)) {
+ return false;
+ }
+ return ((ObjectElem) aObj).object == object;
+ }
+
+ public int hashCode() {
+ return object.hashCode();
+ }
+ }
}