okay ...i could provide some more information now and would be glad if someone could tell me why that doesn't work with 'saveOrUpdateCopy' ...
mapping-files
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>
Used Java-CodeCode:
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();
And parts of the
log-outputCode:
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"
if from interest i have a testcase ...
Sure it could be, that i'm doing something really wrong ... therefore it would be great if someone would take a look ...
TiA
curio