1 /*
2 * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3 * the spring framework, http://el4j.sf.net
4 * Copyright (C) 2008 by ELCA Informatique SA, Av. de la Harpe 22-24,
5 * 1000 Lausanne, Switzerland, http://www.elca.ch
6 *
7 * EL4J is published under the GNU Lesser General Public License (LGPL)
8 * Version 2.1. See http://www.gnu.org/licenses/
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * For alternative licensing, please contact info@elca.ch
16 */
17 package ch.elca.el4j.services.persistence.hibernate;
18
19 import java.util.HashMap;
20 import java.util.HashSet;
21
22 import org.hibernate.collection.PersistentCollection;
23 import org.hibernate.collection.PersistentMap;
24 import org.hibernate.collection.PersistentSet;
25 import org.hibernate.event.PostCollectionRecreateEvent;
26 import org.hibernate.event.PostCollectionRecreateEventListener;
27 import org.hibernate.event.PostCollectionUpdateEvent;
28 import org.hibernate.event.PostCollectionUpdateEventListener;
29 import org.springframework.util.Assert;
30
31 import ch.elca.el4j.services.persistence.generic.dto.AbstractIntKeyIntOptimisticLockingDto;
32
33 /**
34 * This Hibernate event listener refreshes all persisted sets and maps. This is a workaround for all entities based
35 * on {@link AbstractIntKeyIntOptimisticLockingDto} because they change their hash code during the persistence process.
36 * Therefore collections based on hash code have to be refreshed such that the new hash code is used. Otherwise inserted
37 * entities won't be found anymore after persisting.
38 *
39 * @svnLink $Revision: 3875 $;$Date: 2009-08-04 14:35:53 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/hibernate/src/main/java/ch/elca/el4j/services/persistence/hibernate/HibernatePersistentCollectionRefresher.java $
40 *
41 * @author Stefan Wismer (SWI)
42 */
43 public class HibernatePersistentCollectionRefresher implements PostCollectionRecreateEventListener,
44 PostCollectionUpdateEventListener {
45
46 /** {@inheritDoc} */
47 public void onPostRecreateCollection(PostCollectionRecreateEvent event) {
48 refreshCollection(event.getCollection());
49 }
50
51 /** {@inheritDoc} */
52 public void onPostUpdateCollection(PostCollectionUpdateEvent event) {
53 refreshCollection(event.getCollection());
54 }
55
56 /**
57 * Refresh a collection (reorganize it due to possibly changed hash codes).
58 * @param collection the collection to update
59 */
60 @SuppressWarnings("unchecked")
61 private void refreshCollection(PersistentCollection collection) {
62 Assert.isTrue(!collection.isDirty());
63
64 if (collection instanceof PersistentSet) {
65 PersistentSet set = (PersistentSet) collection;
66 HashSet tmpSet = new HashSet(set);
67
68 set.clear();
69 set.addAll(tmpSet);
70 set.postAction();
71 } else if (collection instanceof PersistentMap) {
72 PersistentMap map = (PersistentMap) collection;
73 HashMap tmpMap = new HashMap(map);
74
75 map.clear();
76 map.putAll(tmpMap);
77 map.postAction();
78 }
79 }
80 }