Here is a brief description of what I am trying to do and what the problem is. Any inputs would be greatly appreciated. Thanks !!
Code:
Hibernate Version: 3.1.2
JVM: IBM 1.4.2
Server: WAS 5.1.1
Platform windows
Hibernate Config:
Code:
<property name="show_sql">true</property>
<property name="hibernate.connection.provider_class">org.hibernate.connection.DatasourceConnectionProvider</property>
<property name="hibernate.connection.datasource">jdbc/bph</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
<property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.WebSphereTransactionManagerLookup</property>
<property name="hibernate.transaction.flush_before_completion">false</property>
<property name="hibernate.transaction.auto_close_session">true</property>
I have 2 EJBs.
In EJB1, I load an entity and update it (this works fine).
EJB1 calls EJB2 (remote interface) passing the persistent entity across the remote boundary.
EJB2 runs inside the same transaction (and therefore same hibernate session) as initiated by EJB1. EJB2 does some more changes to the entity and attempts to update it. Now, here is the problem: I get a NonUniqueObjectException. After doing a lot of debugging, I came to the following conclusions. When an entity is passed from EJB1 to EJB2, it's identity hashcode has changed. The Entity which was loaded in EJB1 is just not the same as the entity being updated in EJB2. Hibernate uses System.identityHashCode(Object) to store entities in IdentityMap. When I serialize the entity from EJB1 to EJB2, it's identity hashcode has changed. The following code snippet is taken from IdentityMap.IdentityKey
Code:
public int hashCode() {
return System.identityHashCode(key);
}
When I try to update (session.update(Object)) in EJB2, one of the lines executed in DefaultUpdateEventListener.performSaveOrUpdate(SaveOrUpdateEvent) is
Code:
EntityEntry entry = event.getSession().getPersistenceContext().getEntry( event.getEntity() );
and this returns null (as identity hashcode has changed on serialization, the map cannot locate it). The conclusion is - entity Is Detached.
But Why ?So, it then goes further to find out that the entity is not unique since the follwing line throws the NonUniqueObjectException.
Code:
source.getPersistenceContext().checkUniqueness( key, entity );
in method DefaultSaveOrUpdateEventListener.performUpdate(..).
Possible solutions....
I have also tried to use session.contains(entity) but it returns false, since it uses the same logic of finding out entity entries (entityEntries.containsKey(entity) and identity hashcode).
I was hoping to see something like session.contains(class, key) or session.contains(object), that can actually tell me if an entity already exists in session or not!
Any opinions that can help me resolve this problem would be highly appreciated.
Thanks in advance !