I'm testing parent - child relationships, and trying to insert two new child
rows in the same transaction. Hibernate raises the following exception message.
" a different object with the same identifier value was already associated with the session: [pctest.Child#0]" . Inserting a single child record works fine.
I've modeled my test using the simple Parent/Child from the documentation example:
<hibernate-mapping>
<class name="pctest.Parent" table="parent" catalog="sampdb1">
<comment></comment>
<id name="id" type="long">
<column name="id" />
<generator class="assigned" />
</id>
<property name="parentName" type="string">
<column name="parent_name" length="40">
<comment></comment>
</column>
</property>
<set name="childs" inverse="true" cascade="all" >
<key>
<column name="parent_id" not-null="true">
<comment></comment>
</column>
</key>
<one-to-many class="pctest.Child" />
</set>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="pctest.Child" table="child" catalog="sampdb1">
<comment></comment>
<id name="id" type="long">
<column name="id" />
<generator class="assigned" />
</id>
<many-to-one name="parent" class="pctest.Parent" fetch="select">
<column name="parent_id" not-null="true">
<comment></comment>
</column>
</many-to-one>
<property name="childName" type="string">
<column name="child_name" length="40">
<comment></comment>
</column>
</property>
</class>
</hibernate-mapping>
Here is my Java code:
// The Parent record already exists...
Child child1 = new Child();
child1.setChildName("Sue");
parent.addChild(child1); // parent add child, and child get a reference to it's parent
Child child2 = new Child();
child2.setChildName("Sam");
parent.addChild(child2);
session.flush();
I have tried implementing equals and hashCode on the Child object to use the names;
but that does not solve the problem.
What am I doing wrong? Any help would be greatly appreciated. Thanks.
This is Hibernate 3.2, on MySQL 5
Viewing Parents
Hibernate: select this_.id as id2_0_, this_.state_code as state2_2_0_, this_.parent_name as parent3_2_0_ from sampdb1.parent this_
Parent = Parent1
Hibernate: select children0_.parent_id as parent2_1_, children0_.id as id1_, children0_.id as id1_0_, children0_.parent_id as parent2_1_0_, children0_.child_name as child3_1_0_ from sampdb1.child children0_ where children0_.parent_id=?
Hibernate: select child_.id, child_.parent_id as parent2_1_, child_.child_name as child3_1_ from sampdb1.child child_ where child_.id=?
Exception in thread "main" org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [pctest.Child#0]
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:167)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:98)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:507)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:499)
at org.hibernate.engine.CascadingAction$1.cascade(CascadingAction.java:218)
at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:268)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:216)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:296)
at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:242)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:219)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
at org.hibernate.engine.Cascade.cascade(Cascade.java:130)
at org.hibernate.event.def.AbstractFlushingEventListener.cascadeOnFlush(AbstractFlushingEventListener.java:131)
at org.hibernate.event.def.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:122)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:65)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at pctest.Main.listParents(Main.java:38)
at pctest.Main.main(Main.java:61)
Thanks for any help.
_________________ grant
|