-->
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.  [ 3 posts ] 
Author Message
 Post subject: NonUniqueObjectException when 2 objects refer to same object
PostPosted: Thu Jul 07, 2005 10:59 am 
Newbie

Joined: Wed May 25, 2005 11:03 am
Posts: 13
Hi,

I have a problem where two different objects (A & B) are referring to a third object (C) and B is contained in A.

A------- C
|
B------- C


Save is fired on A. I use lifecycle methods on A to save B. i.e. in lifecycle method "onUpdate" of A, I have written

[b]session.saveOrUpdate(getB())[/b]

While saving it throws exception

Caused by: net.sf.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: C

I tried writing

[b]session.flush() [/b]

immediaely after

[b]session.saveOrUpdate(getB())[/b]

Still the same exception is thrown.

I need help ASAP. Thanking in advance.

Regards


Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

[b]Hibernate version:[/b]

[b]Mapping documents:[/b]

[b]Code between sessionFactory.openSession() and session.close():[/b]

[b]Full stack trace of any exception that occurs:[/b]

[b]Name and version of the database you are using:[/b]

[b]The generated SQL (show_sql=true):[/b]

[b]Debug level Hibernate log excerpt:[/b]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 07, 2005 11:06 am 
Beginner
Beginner

Joined: Wed Jul 06, 2005 8:18 pm
Posts: 23
You should just let Hibernate managing the dependencies for you (witth cascade option). In you case, I guess you tried to save (create) an object that Hibernate already saved. More code + xml mappings would help here.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 07, 2005 11:39 am 
Newbie

Joined: Wed May 25, 2005 11:03 am
Posts: 13
HI,

Thanx a lot for your promp reply. The thing is I CANNOT use hibernate cascade for A---B relationship. Cannot provide much code due to privacy issue. Anyway I did a workaround. I opened one more hibernate session in onUpdate and written following code:

SessionFactory sessionFactory = ServiceLocator.getInstance().getHibernateSessionFactory();

Session newSession = sessionFactory.openSession(session.connection());
newSession.saveOrUpdate(getB());
newSession.flush();
newSession.close();
session.refresh(this);

This worked perfectly.

Please let me know if you have a better option.
Thanx again


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.