X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=support%2Fsrc%2Forg%2Fwamblee%2Fpersistence%2Fhibernate%2FHibernateSupport.java;h=65160dd1a8b2cc76df35e7c37ace8189cc44da00;hb=5b6b49fbe01397461e67238481c84a62c8ed4bef;hp=29debe78a842360a83809580c79703624284962f;hpb=a9a399fb3921ce5dca15cf85cd06f523724a7db1;p=utils diff --git a/support/src/org/wamblee/persistence/hibernate/HibernateSupport.java b/support/src/org/wamblee/persistence/hibernate/HibernateSupport.java index 29debe78..65160dd1 100644 --- a/support/src/org/wamblee/persistence/hibernate/HibernateSupport.java +++ b/support/src/org/wamblee/persistence/hibernate/HibernateSupport.java @@ -34,27 +34,30 @@ import org.wamblee.persistence.Persistent; * {@link org.springframework.orm.hibernate.support.HibernateDaoSupport}. */ 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. - * + * 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; + _object = aObject; } - public boolean equals(Object obj) { - return ((ObjectElem)obj)._object == _object; + + public boolean equals(Object aObj) { + return ((ObjectElem) aObj)._object == _object; } + public int hashCode() { - return _object.hashCode(); + return _object.hashCode(); } } @@ -72,60 +75,67 @@ public class HibernateSupport extends HibernateDaoSupport { * 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. + * 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. + * @param aPersistent + * Object to merge. */ public void merge(Persistent aPersistent) { merge(getHibernateTemplate(), aPersistent); } - + /** - * As {@link #merge(Persistent)} but with a given template. - * This method can be accessed in a static way. - * @param aTemplate Hibernate template - * @param aPersistent Object to merge. + * As {@link #merge(Persistent)} but with a given template. This method can + * be accessed in a static way. + * + * @param aTemplate + * Hibernate template + * @param aPersistent + * Object to merge. */ public static void merge(HibernateTemplate aTemplate, Persistent aPersistent) { - Persistent merged = (Persistent) aTemplate.merge( - aPersistent); + Persistent merged = (Persistent) aTemplate.merge(aPersistent); processPersistent(aPersistent, merged, new ArrayList()); } - - /** - * Copies primary keys and version from the result of the merged to the + * 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 properties of the object. It copies the primary key and version for + * the properties of the object. It copies the primary key and version for * objects that implement {@link Persistent} and applies the same rules to - * objects in maps and sets as well (i.e. recursively). - * @param aPersistent Object whose primary key and version are to be set. - * @param aMerged Object that was the result of the merge. - * @param aProcessed List of already processed Persistent objects of the persistent part. + * objects in maps and sets as well (i.e. recursively). + * + * @param aPersistent + * Object whose primary key and version are to be set. + * @param aMerged + * Object that was the result of the merge. + * @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 ) { - return; + 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. + ObjectElem elem = new ObjectElem(aPersistent); + if (aProcessed.contains(elem)) { + return; // already processed. } aProcessed.add(elem); - + LOG.info("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()); @@ -141,24 +151,29 @@ public class HibernateSupport extends HibernateDaoSupport { Set merged = (Set) getter.invoke(aMerged); Set persistent = (Set) getter.invoke(aPersistent); processSet(persistent, merged, aProcessed); - } else if ( List.class.isAssignableFrom(returnType)) { + } else if (List.class.isAssignableFrom(returnType)) { List merged = (List) getter.invoke(aMerged); List persistent = (List) getter.invoke(aPersistent); processList(persistent, merged, aProcessed); - } else if ( Map.class.isAssignableFrom(returnType)) { + } else if (Map.class.isAssignableFrom(returnType)) { Map merged = (Map) getter.invoke(aMerged); Map persistent = (Map) getter.invoke(aPersistent); processMap(persistent, merged, aProcessed); - } else if ( Persistent.class.isAssignableFrom(returnType)) { - Persistent merged = (Persistent) getter.invoke(aMerged); - Persistent persistent = (Persistent) getter.invoke(aPersistent); + } else if (Persistent.class.isAssignableFrom(returnType)) { + Persistent merged = (Persistent) getter.invoke(aMerged); + Persistent persistent = (Persistent) getter + .invoke(aPersistent); processPersistent(persistent, merged, aProcessed); - } else if ( returnType.isArray() && - Persistent.class.isAssignableFrom(returnType.getComponentType())) { - Persistent[] merged = (Persistent[]) getter.invoke(aMerged); - Persistent[] persistent = (Persistent[]) getter.invoke(aPersistent); - for (int i = 0; i < persistent.length; i++) { - processPersistent(persistent[i], merged[i], aProcessed); + } else if (returnType.isArray() + && Persistent.class.isAssignableFrom(returnType + .getComponentType())) { + Persistent[] merged = (Persistent[]) getter + .invoke(aMerged); + Persistent[] persistent = (Persistent[]) getter + .invoke(aPersistent); + for (int i = 0; i < persistent.length; i++) { + processPersistent(persistent[i], merged[i], + aProcessed); } } } catch (InvocationTargetException e) { @@ -172,12 +187,17 @@ public class HibernateSupport extends HibernateDaoSupport { } /** - * Process the persistent objects in the collections. - * @param aPersistent Collection in the original object. - * @param aMerged Collection as a result of the merge. - * @param aProcessed List of processed persistent objects. + * Process the persistent objects in the collections. + * + * @param aPersistent + * Collection in the original object. + * @param aMerged + * Collection as a result of the merge. + * @param aProcessed + * List of processed persistent objects. */ - public static void processList(List aPersistent, List aMerged, List aProcessed) { + public static void processList(List aPersistent, List aMerged, + List aProcessed) { Object[] merged = aMerged.toArray(); Object[] persistent = aPersistent.toArray(); if (merged.length != persistent.length) { @@ -187,55 +207,70 @@ public class HibernateSupport extends HibernateDaoSupport { 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); + processPersistent((Persistent) persistent[i], + (Persistent) merged[i], aProcessed); } } } - + /** - * Process the persistent objects in sets. - * @param aPersistent Collection in the original object. - * @param aMerged Collection as a result of the merge. - * @param aProcessed List of processed persistent objects. + * Process the persistent objects in sets. + * + * @param aPersistent + * Collection in the original object. + * @param aMerged + * Collection as a result of the merge. + * @param aProcessed + * List of processed persistent objects. */ - public static void processSet(Set aPersistent, Set aMerged, List aProcessed) { + public static void processSet(Set aPersistent, Set aMerged, + List aProcessed) { if (aMerged.size() != aPersistent.size()) { throw new RuntimeException("Array sizes differ " + aMerged.size() + " " + aPersistent.size()); } - for (Object merged: aMerged) { + 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); + for (Object persistent : aPersistent) { + if (persistent.equals(merged)) { + processPersistent((Persistent) persistent, + (Persistent) merged, aProcessed); break; } } } } - + /** - * Process the Map objects in the collections. - * @param aPersistent Collection in the original object. - * @param aMerged Collection as a result of the merge. - * @param aProcessed List of processed persistent objects. + * Process the Map objects in the collections. + * + * @param aPersistent + * Collection in the original object. + * @param aMerged + * Collection as a result of the merge. + * @param aProcessed + * List of processed persistent objects. */ - public static void processMap(Map aPersistent, Map aMerged, List aProcessed) { - if ( aMerged.size() != aPersistent.size() ) { - throw new RuntimeException("Sizes differ " + aMerged.size() + " " + aPersistent.size()); + public static void processMap(Map aPersistent, Map aMerged, + List aProcessed) { + if (aMerged.size() != aPersistent.size()) { + throw new RuntimeException("Sizes differ " + aMerged.size() + " " + + aPersistent.size()); } Set keys = aMerged.keySet(); - for (Object key: keys) { - if ( !aPersistent.containsKey(key) ) { - throw new RuntimeException("Key '" + key + "' not found"); + for (Object key : keys) { + if (!aPersistent.containsKey(key)) { + throw new RuntimeException("Key '" + key + "' not found"); } - Object mergedValue = aMerged.get(key); - Object persistentValue = aPersistent.get(key); - if ( mergedValue instanceof Persistent ) { - if ( persistentValue instanceof Persistent ) { - processPersistent((Persistent)persistentValue, (Persistent)mergedValue, aProcessed); + Object mergedValue = aMerged.get(key); + Object persistentValue = aPersistent.get(key); + if (mergedValue instanceof Persistent) { + if (persistentValue instanceof Persistent) { + processPersistent((Persistent) persistentValue, + (Persistent) mergedValue, aProcessed); } else { - throw new RuntimeException("Value in original object is null, whereas merged object contains a value"); + throw new RuntimeException( + "Value in original object is null, whereas merged object contains a value"); } } }