Revision ce9bc492
Added by Cherian Mathew over 8 years ago
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/hibernate/PostMergeEntityListener.java | ||
---|---|---|
9 | 9 |
*/ |
10 | 10 |
package eu.etaxonomy.cdm.persistence.hibernate; |
11 | 11 |
|
12 |
import java.util.HashSet; |
|
12 | 13 |
import java.util.Map; |
14 |
import java.util.Set; |
|
15 |
import java.util.concurrent.ConcurrentHashMap; |
|
13 | 16 |
|
14 | 17 |
import org.hibernate.Hibernate; |
15 | 18 |
import org.hibernate.HibernateException; |
19 |
import org.hibernate.Session; |
|
16 | 20 |
import org.hibernate.event.spi.MergeEvent; |
17 | 21 |
import org.hibernate.event.spi.MergeEventListener; |
18 | 22 |
|
... | ... | |
25 | 29 |
*/ |
26 | 30 |
public class PostMergeEntityListener implements MergeEventListener { |
27 | 31 |
|
32 |
private static Map<Session, Set<CdmBase>> newEntitiesMap = new ConcurrentHashMap<Session, Set<CdmBase>>(); |
|
33 |
|
|
34 |
public static void addSession(Session session) { |
|
35 |
newEntitiesMap.put(session, new HashSet<CdmBase>()); |
|
36 |
} |
|
37 |
|
|
38 |
public static void removeSession(Session session) { |
|
39 |
newEntitiesMap.remove(session); |
|
40 |
} |
|
41 |
|
|
42 |
public static Set<CdmBase> getNewEntities(Session session) { |
|
43 |
return newEntitiesMap.get(session); |
|
44 |
} |
|
28 | 45 |
|
29 | 46 |
|
30 | 47 |
/* (non-Javadoc) |
... | ... | |
40 | 57 |
*/ |
41 | 58 |
@Override |
42 | 59 |
public void onMerge(MergeEvent event, Map copiedAlready) throws HibernateException { |
43 |
// at this point the original entity to merge has already been copied to the result |
|
44 |
// => the result is an exact copy of the original EXCEPT or the the id which is set by hibernate |
|
45 |
// the following code sets the id in the original entity so that it can be used as a return value |
|
46 |
// for the CdmEntityDaoBase.merge(T transientObject, boolean returnTransientEntity) call |
|
60 |
// any new entities are added to a map which is retrieved at the end of the |
|
61 |
// CdmEntityDaoBase.merge(T transientObject, boolean returnTransientEntity) call |
|
47 | 62 |
if(event.getOriginal() != null && CdmBase.class.isAssignableFrom(event.getOriginal().getClass()) && |
48 | 63 |
event.getResult() != null && CdmBase.class.isAssignableFrom(event.getResult().getClass())) { |
49 | 64 |
CdmBase original = (CdmBase) event.getOriginal(); |
... | ... | |
51 | 66 |
if(original != null && Hibernate.isInitialized(original) && original.getId() == 0 && |
52 | 67 |
result != null && Hibernate.isInitialized(result) && result.getId() > 0) { |
53 | 68 |
original.setId(result.getId()); |
54 |
//FIXME: Once the EventType.SAVE_UPDATE listeners are cleaned up |
|
55 |
// the same calls should be made on the result object |
|
56 |
// followed by a copy (uncomment code below) to the |
|
57 |
// original object |
|
58 |
// try { |
|
59 |
// BeanUtils.copyProperties(original, result); |
|
60 |
// } catch (IllegalAccessException e) { |
|
61 |
// // TODO Auto-generated catch block |
|
62 |
// e.printStackTrace(); |
|
63 |
// } catch (InvocationTargetException e) { |
|
64 |
// // TODO Auto-generated catch block |
|
65 |
// e.printStackTrace(); |
|
66 |
// } |
|
69 |
Set<CdmBase> newEntities = newEntitiesMap.get(event.getSession()); |
|
70 |
if(newEntities != null) { |
|
71 |
newEntities.add(result); |
|
72 |
} |
|
67 | 73 |
} |
68 | 74 |
} |
69 | 75 |
} |
Also available in: Unified diff
Add new merge method for root entitites which returns the newly created MergeResult object