I had a NonUniqueObjectException exception when upgrading my project from Hibernate 3 to Hibernate 4. I created a minimal Hibernate 4 project for testing and was able to throw this exception:
with the entity House and another entity. A door that has a composite identifier that contains the House object.
<class dynamic-insert="true" dynamic-update="true" name="entity.Door" select-before-update="true" table="DOOR">
<composite-id name="id" class="DoorHousePK">
<key-many-to-one class="entity.House" column="HOUSEID" name="house"/>
<key-property column="DOORID" name="doorId" type="string"/>
</composite-id>
and then in the same transaction that receives the House, and then retrieving the door by a composite identifier using:
(Door) session.get (Door.class, doorHousePK)
Here is the hbm file for home:
<class dynamic-insert="true" dynamic-update="true" name="entity.House" select-before-update="true" table="HOUSE">
<id column="ID" name="id" type="int"/>
<property column="squarefeet" name="squareFeet" not-null="false" type="int"/>
<property column="address" length="255" name="address" not-null="false" type="string"/>
<property column="color" length="32" name="color" not-null="false" type="string"/>
<property column="description" name="description" not-null="false" type="materialized_clob"/>
</class>
and error stack trace:
INFO: HHH000327: Error performing load command : org.hibernate.NonUniqueObjectException: A different object with the same identifier value was already associated with the session : [entity.House
Exception in thread "main" org.hibernate.NonUniqueObjectException: A different object with the same identifier value was already associated with the session : [entity.House
at org.hibernate.engine.internal.StatefulPersistenceContext.checkUniqueness(StatefulPersistenceContext.java:617)
at org.hibernate.event.internal.AbstractReassociateEventListener.reassociate(AbstractReassociateEventListener.java:74)
at org.hibernate.event.internal.DefaultLockEventListener.onLock(DefaultLockEventListener.java:95)
at org.hibernate.internal.SessionImpl.fireLock(SessionImpl.java:774)
at org.hibernate.internal.SessionImpl.fireLock(SessionImpl.java:767)
at org.hibernate.internal.SessionImpl.access$1800(SessionImpl.java:176)
at org.hibernate.internal.SessionImpl$LockRequestImpl.lock(SessionImpl.java:2491)
at org.hibernate.loader.plan.exec.internal.EntityLoadQueryDetails$EntityLoaderRowReader.addKeyManyToOnesToSession(EntityLoadQueryDetails.java:263)
at org.hibernate.loader.plan.exec.internal.EntityLoadQueryDetails$EntityLoaderRowReader.readRow(EntityLoadQueryDetails.java:247)
at org.hibernate.loader.plan.exec.process.internal.ResultSetProcessorImpl.extractResults(ResultSetProcessorImpl.java:129)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:138)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:102)
at org.hibernate.loader.entity.plan.AbstractLoadPlanBasedEntityLoader.load(AbstractLoadPlanBasedEntityLoader.java:186)
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:4126)
at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:503)
at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:468)
at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:213)
at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:275)
at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:151)
at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1070)
at org.hibernate.internal.SessionImpl.access$2000(SessionImpl.java:176)
at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.load(SessionImpl.java:2551)
at org.hibernate.internal.SessionImpl.get(SessionImpl.java:955)
When this minimal project is returned back to Hibernate 3, the exception disappears and everything works fine.
What is the difference in handling composite identifiers between Hibernate 3 and Hibernate 4? How can I make this work in Hibernate 4?