I have a user and credit_card entity, user has a collection of credit_card. The primary key for both of these entities i have is a byte[].
First session: - I opened a new session. - Do criteria query on the users table, which actually goes to the DB and fetches user + credit_cards and finally i have a user Object say userObj. - Close the session.
Second session: - I Opened another session. - trying to invoke session.buildLockRequest(LockOptions.NONE).lock(userObj);
Now hibernate will try to reattach the detached object, but i get following error:
org.hibernate.HibernateException: reassociated object has dirty collection reference
at org.hibernate.event.def.OnLockVisitor.processCollection(OnLockVisitor.java:71)
at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:122)
at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:83)
at org.hibernate.event.def.AbstractVisitor.processEntityPropertyValues(AbstractVisitor.java:77)
at org.hibernate.event.def.AbstractVisitor.process(AbstractVisitor.java:144)
at org.hibernate.event.def.AbstractReassociateEventListener.reassociate(AbstractReassociateEve ntListener.java:101)
at org.hibernate.event.def.DefaultLockEventListener.onLock(DefaultLockEventListener.java:82)
at org.hibernate.impl.SessionImpl.fireLock(SessionImpl.java:774)
at org.hibernate.impl.SessionImpl.fireLock(SessionImpl.java:766)
at org.hibernate.impl.SessionImpl.access$600(SessionImpl.java:156)
I tried to debug it in the hibernate code, and found out, that hibernate tries to check if the passed object's collection (credit_cards) is actually owned by user (which is user1).
So somewhere deep down the code in hibernate, it seems like it is checking for the equality with equals method of the primary keys of the passed object (which is user1) and another object what it is calling a snapshot object. Since byte[] is essentially an array and fails on equals check and throw the above error. I know i can do above work in just one session, but this is just a scenario.
I tried using Long/Integer as a primary key and it works just fine, since it passes the equality check.
Hibernate version : 3.6.9.Final (i tried to take a look at the 4.1.1.Final version as well, but files/code which throws this error are not changed) DB : SQL server
Is it a issue in hibernate or i am doing anything wrong ?