Hibernate version: 2.1.7
Mapping documents:
Code:
<hibernate-mapping>
<class
name="test.hibernate.Box"
table="box">
<id
name="boxID"
column="bid"
unsaved-value="null"
access="field"
type="integer">
<generator class="native"/>
</id>
<version name="version" access="field" column="version" type="integer"/>
<property
name="boxNumber"
column="boxNr"
type="integer"/>
<set
name="toys"
table="toy"
inverse="true"
outer-join="true"
cascade="save-update">
<key column="box_bid"/>
<one-to-many class="test.hibernate.Toy"/>
</set>
<many-to-one
name="delivery"
column="Delivery_did"
class="test.hibernate.Delivery"
outer-join="true"/>
</class>
</hibernate-mapping>
Code:
<hibernate-mapping>
<class
name="test.hibernate.Delivery"
table="delivery">
<id
name="deliveryID"
column="did"
unsaved-value="null"
access="field"
type="integer">
<generator class="native"/>
</id>
<version name="version" access="field" column="version" type="integer"/>
<property
name="deliveryDate"
column="delDate"
type="date"/>
<set
name="boxes"
table="box"
inverse="true"
outer-join="true"
cascade="save-update">
<key column="delivery_did"/>
<one-to-many class="test.hibernate.Box"/>
</set>
<set
name="toys"
table="toy"
inverse="true">
<key column="delivery_did"/>
<one-to-many class="test.hibernate.Toy"/>
</set>
</class>
</hibernate-mapping>
Code:
<hibernate-mapping>
<class
name="test.hibernate.Toy"
table="toy">
<id
name="toyID"
column="tid"
unsaved-value="null"
access="field"
type="integer">
<generator class="native"/>
</id>
<version name="version" access="field" column="version" type="integer"/>
<property
name="description"
column="descr"
type="string"/>
<many-to-one
name="delivery"
column="delivery_did"
class="test.hibernate.Delivery"
not-null="true"/>
<many-to-one
name="box"
column="box_bid"
class="test.hibernate.Box"
not-null="false"/>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():testCode:
Code:
BasicConfigurator.configure();
System.out.println("Starting test ...");
Configuration config = new Configuration();
SessionFactory sf = config.configure().buildSessionFactory();
System.out.println("Loading Delivery ...");
Session s = sf.openSession();
Delivery d = (Delivery) s.find("from Delivery").get(0);
s.close();
s = sf.openSession();
Delivery origDelivery = (Delivery) s.find("from Delivery").get(0);
s.close();
s = sf.openSession();
Transaction t = s.beginTransaction();
// create new box and add it to delivery and add a toy
Box b = new Box();
b.setDelivery(d);
d.getBoxes().add(b);
Toy toy = (Toy) d.getToys().iterator().next();
toy.setBox(b);
b.getToys().add(toy);
// lock original delivery
s.lock(origDelivery, LockMode.NONE);
// save the changed delivery
s.saveOrUpdateCopy(d);
t.commit();
s.close();
Name and version of the database you are using:IBM DB2 AS400 V5R2 and MySQL current version in Windows
Debug level Hibernate log excerpt:Code:
21515 [main] DEBUG net.sf.hibernate.engine.Cascades - processing cascades for: test.hibernate.Box
21515 [main] DEBUG net.sf.hibernate.engine.Cascades - cascading to collection: test.hibernate.Box.toys
21515 [main] DEBUG net.sf.hibernate.engine.Cascades - done processing cascades for: test.hibernate.Box
21515 [main] DEBUG net.sf.hibernate.engine.Cascades - processing cascades for: test.hibernate.Box
21515 [main] DEBUG net.sf.hibernate.engine.Cascades - cascading to collection: test.hibernate.Box.toys
21515 [main] DEBUG net.sf.hibernate.engine.Cascades - cascading to saveOrUpdate()
21515 [main] DEBUG net.sf.hibernate.engine.Cascades - version unsaved-value strategy UNDEFINED
21515 [main] DEBUG net.sf.hibernate.engine.Cascades - id unsaved-value strategy NULL
21515 [main] DEBUG net.sf.hibernate.impl.SessionImpl - saveOrUpdate() previously saved instance with id: 37
21515 [main] DEBUG net.sf.hibernate.impl.SessionImpl - updating [test.hibernate.Toy#37]
net.sf.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: 37, of class: test.hibernate.Toy
at net.sf.hibernate.impl.SessionImpl.checkUniqueness(SessionImpl.java:1687)
at net.sf.hibernate.impl.SessionImpl.doUpdateMutable(SessionImpl.java:1470)
at net.sf.hibernate.impl.SessionImpl.doUpdate(SessionImpl.java:1490)
at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1419)
at net.sf.hibernate.engine.Cascades$4.cascade(Cascades.java:115)
at test.hibernate.TestHibernate.main(TestHibernate.java:62)
Exception in thread "main"
Hi,
made some tests during the weekend ... but i'm not able to figure my mistake out ... perhaps i'm trying something not supported?
Situation:
There's a
delivery containing a lot of
toys (bidirectional 1:n). Each
toy will be analyzed ... after analyzing a
toy it'll be thrown into a
box (bidirectional 0:n). After this work is done, i've got a lot of boxes for one delivery (bidirectional 0:n).
Code:
delivery (1)<--------------------->(1:n) toy
box (1)<--------------------->(0:n) toy
delivery (1)<---------------------->(0:n) box
For historizing all changes and working with detached objects, i'm using
session.lock(delivery, LockMode.NONE) to reattach the loaded, unchanged delivery. After that i'm using 'saveOrUpdateCopy' with the changed delivery ... and the Exception above occurs.
As you can see in the code, i'm creating a new box ... associate a delivery and toy .... No second-level-cache.
Using
saveOrUpdate instead of
saveOrUpdateCopy works perfectly ...
Not creating a new box (just changing a toy) works, too ...
Someone knows what i'm doing wrong? Isn't that possible in that way i'm trying it?
TiA
curio