2 * Copyright 2005 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.wamblee.persistence.hibernate;
19 import java.lang.reflect.InvocationTargetException;
20 import java.lang.reflect.Method;
21 import java.util.ArrayList;
22 import java.util.List;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.springframework.orm.hibernate3.HibernateTemplate;
29 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
30 import org.wamblee.persistence.Persistent;
34 * {@link org.springframework.orm.hibernate.support.HibernateDaoSupport}.
36 public class HibernateSupport extends HibernateDaoSupport {
38 private static final Log LOG = LogFactory.getLog(HibernateSupport.class);
41 * This class provided an equality operation based on the object reference
42 * of the wrapped object. This is required because we cannto assume that the
43 * equals operation has any meaning for different types of persistent
44 * objects. This allows us to use the standard collection classes for
45 * detecting cyclic dependences and avoiding recursion.
48 private static final class ObjectElem {
49 private Object _object;
51 public ObjectElem(Object aObject) {
55 public boolean equals(Object aObj) {
56 return ((ObjectElem) aObj)._object == _object;
59 public int hashCode() {
60 return _object.hashCode();
65 * Constructs the object.
68 public HibernateSupport() {
73 * Performes a hibernate <code>Session.merge()</code> and updates the
74 * object with the correct primary key and version. This is an extension to
75 * the hibernate merge operation because hibernate itself leaves the object
76 * passed to merge untouched.
78 * Use this method with extreme caution since it will recursively load all
79 * objects that the current object has relations with and for which
80 * cascade="merge" was specified in the Hibernate mapping file.
85 public void merge(Persistent aPersistent) {
86 merge(getHibernateTemplate(), aPersistent);
90 * As {@link #merge(Persistent)} but with a given template. This method can
91 * be accessed in a static way.
98 public static void merge(HibernateTemplate aTemplate, Persistent aPersistent) {
99 Persistent merged = (Persistent) aTemplate.merge(aPersistent);
100 processPersistent(aPersistent, merged, new ArrayList<ObjectElem>());
104 * Copies primary keys and version from the result of the merged to the
105 * object that was passed to the merge operation. It does this by traversing
106 * the properties of the object. It copies the primary key and version for
107 * objects that implement {@link Persistent} and applies the same rules to
108 * objects in maps and sets as well (i.e. recursively).
111 * Object whose primary key and version are to be set.
113 * Object that was the result of the merge.
115 * List of already processed Persistent objects of the persistent
118 public static void processPersistent(Persistent aPersistent,
119 Persistent aMerged, List<ObjectElem> aProcessed) {
120 if (aPersistent == null && aMerged == null) {
123 if (aPersistent == null || aMerged == null) {
124 throw new RuntimeException("persistent or merged object is null '"
125 + aPersistent + "'" + " '" + aMerged + "'");
127 ObjectElem elem = new ObjectElem(aPersistent);
128 if (aProcessed.contains(elem)) {
129 return; // already processed.
131 aProcessed.add(elem);
133 LOG.info("Setting pk/version on " + aPersistent + " from " + aMerged);
135 if (aPersistent.getPrimaryKey() != null
136 && !aMerged.getPrimaryKey().equals(aPersistent.getPrimaryKey())) {
137 LOG.error("Mismatch between primary key values: " + aPersistent
140 aPersistent.setPersistedVersion(aMerged.getPersistedVersion());
141 aPersistent.setPrimaryKey(aMerged.getPrimaryKey());
144 Method[] methods = aPersistent.getClass().getMethods();
145 for (Method getter : methods) {
146 if (getter.getName().startsWith("get")) {
147 Class returnType = getter.getReturnType();
150 if (Set.class.isAssignableFrom(returnType)) {
151 Set merged = (Set) getter.invoke(aMerged);
152 Set persistent = (Set) getter.invoke(aPersistent);
153 processSet(persistent, merged, aProcessed);
154 } else if (List.class.isAssignableFrom(returnType)) {
155 List merged = (List) getter.invoke(aMerged);
156 List persistent = (List) getter.invoke(aPersistent);
157 processList(persistent, merged, aProcessed);
158 } else if (Map.class.isAssignableFrom(returnType)) {
159 Map merged = (Map) getter.invoke(aMerged);
160 Map persistent = (Map) getter.invoke(aPersistent);
161 processMap(persistent, merged, aProcessed);
162 } else if (Persistent.class.isAssignableFrom(returnType)) {
163 Persistent merged = (Persistent) getter.invoke(aMerged);
164 Persistent persistent = (Persistent) getter
165 .invoke(aPersistent);
166 processPersistent(persistent, merged, aProcessed);
167 } else if (returnType.isArray()
168 && Persistent.class.isAssignableFrom(returnType
169 .getComponentType())) {
170 Persistent[] merged = (Persistent[]) getter
172 Persistent[] persistent = (Persistent[]) getter
173 .invoke(aPersistent);
174 for (int i = 0; i < persistent.length; i++) {
175 processPersistent(persistent[i], merged[i],
179 } catch (InvocationTargetException e) {
180 throw new RuntimeException(e.getMessage(), e);
181 } catch (IllegalAccessException e) {
182 throw new RuntimeException(e.getMessage(), e);
190 * Process the persistent objects in the collections.
193 * Collection in the original object.
195 * Collection as a result of the merge.
197 * List of processed persistent objects.
199 public static void processList(List aPersistent, List aMerged,
200 List<ObjectElem> aProcessed) {
201 Object[] merged = aMerged.toArray();
202 Object[] persistent = aPersistent.toArray();
203 if (merged.length != persistent.length) {
204 throw new RuntimeException("Array sizes differ " + merged.length
205 + " " + persistent.length);
207 for (int i = 0; i < merged.length; i++) {
208 assert merged[i].equals(persistent[i]);
209 if (merged[i] instanceof Persistent) {
210 processPersistent((Persistent) persistent[i],
211 (Persistent) merged[i], aProcessed);
217 * Process the persistent objects in sets.
220 * Collection in the original object.
222 * Collection as a result of the merge.
224 * List of processed persistent objects.
226 public static void processSet(Set aPersistent, Set aMerged,
227 List<ObjectElem> aProcessed) {
228 if (aMerged.size() != aPersistent.size()) {
229 throw new RuntimeException("Array sizes differ " + aMerged.size()
230 + " " + aPersistent.size());
232 for (Object merged : aMerged) {
233 // Find the object that equals the merged[i]
234 for (Object persistent : aPersistent) {
235 if (persistent.equals(merged)) {
236 processPersistent((Persistent) persistent,
237 (Persistent) merged, aProcessed);
245 * Process the Map objects in the collections.
248 * Collection in the original object.
250 * Collection as a result of the merge.
252 * List of processed persistent objects.
254 public static void processMap(Map aPersistent, Map aMerged,
255 List<ObjectElem> aProcessed) {
256 if (aMerged.size() != aPersistent.size()) {
257 throw new RuntimeException("Sizes differ " + aMerged.size() + " "
258 + aPersistent.size());
260 Set keys = aMerged.keySet();
261 for (Object key : keys) {
262 if (!aPersistent.containsKey(key)) {
263 throw new RuntimeException("Key '" + key + "' not found");
265 Object mergedValue = aMerged.get(key);
266 Object persistentValue = aPersistent.get(key);
267 if (mergedValue instanceof Persistent) {
268 if (persistentValue instanceof Persistent) {
269 processPersistent((Persistent) persistentValue,
270 (Persistent) mergedValue, aProcessed);
272 throw new RuntimeException(
273 "Value in original object is null, whereas merged object contains a value");