this is my setup:
parent A (Derde) ->
1-to-m to child B (DerdeRol) ->
1-to-m to child C (DerdeRolErk)
they have composite-ids, and I implemented an Interceptor like the example in the manual in section 9.4. Inverse=""true", lazy="false",cascade="all" in the mapping files. I used bags as collection.
When testing my methods to interact with the dbase, this is a piece of code to load a child C object (derdeRolErk), update it and persist it:
Derde derde = (Derde)helper.fetchParent(75);
DerdeRol derdeRol = derde.fetchDerdeRol("aaa");
DerdeRolErk loadedErk = derdeRol.fetchDerdeRolErk(new Date(1987654321L));
loadedErk.setRedenDesactiv("updated text");
derdeRol.getErkenningen().set(11,loadedErk);
derde.getRoles().set(2,derdeRol);
derde.updateInDbase();
First I load from dbase parent A, then A.loadchildB from dbase, then
B.loadchildC from dbase, by passing the relevant ids to a hqlString.I update one of the fields not part of the composite-id, and update the child C object in the collection of child B, the update the child B object in the parent A collection. Then I call the parentA.updateInDbase() method and I get this error:
net.sf.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: DerdeRolErkCompId@1bc4ec8[derdeRol=DerdeRol@cb42cf[compId=DerdeRolCompId@77eb97[derde=Derde@6a63d3[derdeId=75],rolId=aaa]],startDatum=1970-01-24 00:00:00.0], of class: DerdeRolErk
at net.sf.hibernate.impl.SessionImpl.checkUniqueness(SessionImpl.java:1605)
at net.sf.hibernate.impl.SessionImpl.doUpdateMutable(SessionImpl.java:1377)
at net.sf.hibernate.impl.SessionImpl.doUpdate(SessionImpl.java:1403)
at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1338)
at net.sf.hibernate.engine.Cascades$4.cascade(Cascades.java:114)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:436)
at net.sf.hibernate.engine.Cascades.cascadeCollection(Cascades.java:526)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:452)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:503)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:482)
at net.sf.hibernate.impl.SessionImpl.doUpdate(SessionImpl.java:1408)
at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1338)
at net.sf.hibernate.engine.Cascades$4.cascade(Cascades.java:114)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:436)
at net.sf.hibernate.engine.Cascades.cascadeCollection(Cascades.java:526)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:452)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:503)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:482)
at net.sf.hibernate.impl.SessionImpl.doUpdate(SessionImpl.java:1408)
at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1338)
**********************************************
The underlying method looks like this:
public HibernateObject fetchFromDb(String hqlString) throws HibernateException {
HibernateObject result = null;
Session session = _sessions.openSession(new MyInterceptor());
Transaction tx = null;
List helplist = null;
try {
log.debug("Start fetchFromDb");
tx = session.beginTransaction();
helplist = session.find(hqlString);
tx.commit();
result = (HibernateObject)helplist.get(0);
}
catch (HibernateException he) {
log.debug("******** Rollback fetchFromDb");
if (tx!=null) tx.rollback();
throw he;
}
finally {
session.close();
log.debug(" ******** End fetchFromDb");
}
return result;
} //end of method fetchFromDb(String hqlString)
****************************************************
public void updateInDbase() throws HibernateException {
Session session = _sessions.openSession(new MyInterceptor());
Transaction tx = null;
try {
log.debug("********* Start update in dbase of object " + this);
tx = session.beginTransaction();
session.saveOrUpdate(this);
tx.commit();
}
catch (HibernateException he) {
log.debug("******** Rollback updateInDbase");
if (tx!=null) tx.rollback();
throw he;
}
finally {
session.close();
log.debug(" ******** End updateInDbase");
}
} // end of method updateInDbase
******************************************************
I'm mising something here, because from my methods I think that all objects are detached from the session since every call end with a session.close ... ?
tx,
kazroh
|