3 * Copyright (C) 2015 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.cdm
.persistence
.hibernate
;
12 import java
.util
.HashSet
;
15 import java
.util
.concurrent
.ConcurrentHashMap
;
17 import org
.hibernate
.Hibernate
;
18 import org
.hibernate
.HibernateException
;
19 import org
.hibernate
.Session
;
20 import org
.hibernate
.event
.spi
.MergeEvent
;
21 import org
.hibernate
.event
.spi
.MergeEventListener
;
23 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
30 public class PostMergeEntityListener
implements MergeEventListener
{
31 private static final long serialVersionUID
= 1565797119368313987L;
33 private static Map
<Session
, Set
<CdmBase
>> newEntitiesMap
= new ConcurrentHashMap
<Session
, Set
<CdmBase
>>();
35 public static void addSession(Session session
) {
36 newEntitiesMap
.put(session
, new HashSet
<CdmBase
>());
39 public static void removeSession(Session session
) {
40 newEntitiesMap
.remove(session
);
43 public static Set
<CdmBase
> getNewEntities(Session session
) {
44 return newEntitiesMap
.get(session
);
48 public void onMerge(MergeEvent event
) throws HibernateException
{
53 public void onMerge(MergeEvent event
, Map copiedAlready
) throws HibernateException
{
54 // any new entities are added to a map which is retrieved at the end of the
55 // CdmEntityDaoBase.merge(T transientObject, boolean returnTransientEntity) call
56 if(event
.getOriginal() != null && CdmBase
.class.isAssignableFrom(event
.getOriginal().getClass()) &&
57 event
.getResult() != null && CdmBase
.class.isAssignableFrom(event
.getResult().getClass())) {
58 CdmBase original
= (CdmBase
) event
.getOriginal();
59 CdmBase result
= (CdmBase
) event
.getResult();
60 if(original
!= null && Hibernate
.isInitialized(original
) && original
.getId() == 0 &&
61 result
!= null && Hibernate
.isInitialized(result
) && result
.getId() > 0) {
62 original
.setId(result
.getId());
63 Set
<CdmBase
> newEntities
= newEntitiesMap
.get(event
.getSession());
64 if(newEntities
!= null) {
65 newEntities
.add(result
);