-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: NonUniqueObjectException: cascadings 1-to-m, composite-id
PostPosted: Fri Feb 20, 2004 5:39 am 
Newbie

Joined: Sat Feb 14, 2004 12:56 pm
Posts: 8
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


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2004 6:10 am 
Newbie

Joined: Sat Feb 14, 2004 12:56 pm
Posts: 8
aditional comment: when doing the same from Parent A to child B, with the same methods, then saveOrUpdate works fine ....




//test hqlstrings for fetchDerdeRol by rolId and parentId
Derde helper = new Derde();
Derde derde = (Derde)helper.fetchParent(75);
DerdeRol derdeRol = derde.fetchDerdeRol("aab");
derdeRol.setPassword("oud");
derde.getRoles().set(3,derdeRol);
derde.updateInDbase();


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2004 7:39 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
did you read the FAQ?

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2004 9:09 am 
Newbie

Joined: Sat Feb 14, 2004 12:56 pm
Posts: 8
yes I went tru the faqs and it was still a puzzle to me ...

In the meantime I found the problem: I was focussing too much on the
correctness of the code for persistency, and I forgot to check the code dealing with transient objects ...

There was a bug in my method that had to control whether or not you are adding duplicate objects to a Collection. so two identical objects in the collection caused the error.

sorry for hitting the panic button too fast !

tx,
kazroh


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.