Please, please help me
Parent.hbm.xml
<class name="package.Parent" select-before-update="true" table="Parent" dynamic-update="true">
<id name="id" column="parentid" type="package.persistence.StringType"
unsaved-value="-1" >
<generator class="assigned" />
</id>
<set name="child" cascade="all" lazy="true" inverse="true">
<key column="Parent_id" />
<one-to-many class="Child" />
</set>
Child.hbm.xlm
<class name="package.Child" select-before-update="true" table="Child" dynamic-update="true">
<id name="id" column="ChildId" type="package.persistence.StringType" unsaved-value="-1" >
<generator class="assigned" />
</id>
Code
session = HibernateSessionFactory.currentSession();
tx = session.beginTransaction();
parent = (Parent) session.load(Parent.class, "999999");
parent.getChild().size();
session.flush();
tx.commit();
session.close();
// Modify the parent data
parent.setCorp("333333");
Child child = new Child();
edcsCase.setId("888888"); // Comment out then inserted with -1, uncommented gives Stale exception
child.setParent(parent);
if (parent.getChild() == null) {
parent.setChild(new HashSet());
}
parent.getChild().add(child);
session = HibernateSessionFactory.currentSession();
tx = session.beginTransaction();
session.update(parent); // update at root level
session.flush();
tx.commit();
session.close();
|