-->
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: error with NonUniqueObjectException: please, help me...
PostPosted: Tue Jan 27, 2009 8:26 am 
Newbie

Joined: Mon May 14, 2007 9:58 pm
Posts: 7
Location: Bahia / Brasil
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.0

Full stack trace of any exception that occurs: Caused by: org.hibernate.NonUniqueObjectException: a different object
with the same identifier value was already associated with the session:
[B#1]

Name and version of the database you are using: postgresql-8.2



I have two objects: A and B:

===============================
class A {
private B b;
private ArrayList blist;

public void setB(B b) {
this.b = b;
}

public void addB(B b) {
blist.add(b);
}
}
===============================

Well..., I'm loading that objects from data base like this:

===============================
A a = new A();

sessionFactory.openSession();
b1 = servico.getB(1);
a.setB(b1);
session.close();

//Faço um monte de coisa... bla bla bla...

sessionFactory.openSession();
b1 = servico.getB(1);
a.addB(b1);
session.close();

//Faço um monte de coisa... bla bla bla...

sessionFactory.openSession();
servico.gravaA(a);
session.close();
===============================

At line "servico.gravaA(a);" an exception was generated:

===============================
Caused by: org.hibernate.NonUniqueObjectException: a different object
with the same identifier value was already associated with the session:
[B#1]
===============================

So..., what's going on and How can I fix this?

Thank you

Cristiano


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 27, 2009 9:41 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
The problem is that you load the object 'b1' twice with two different sessions and then attach both to the same 'a' object. First with A.setB() and then with A.addB(). Hibernate can't handle this. One solution is to make sure that you use the same session during one unit-of-work. In the code example you provide you can for example call:

Code:
sessionFactory.openSession();
b1 = servico.getB(1);
a.setB(b1);
a.addB(b1);
servico.gravaA(a);
session.close();


Note 1! I don't understand what 'Faço um monte de coisa...' means. Maybe it is impractical to keep the session open during this phase in the code...

Note 2! I think the problem is only triggered if there is a cascade from A to B, because as long as the saving of A doesn't cascade to B there shouldn't be any problem with references to different instances of the same entity. Is it correct that you have a cascade from A to B?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 27, 2009 1:50 pm 
Newbie

Joined: Mon May 14, 2007 9:58 pm
Posts: 7
Location: Bahia / Brasil
Hi nordborg,

thank you for try to help me.

Yes, you understood the situation. Infact the problem that I load the object 'b1' twice with two different sessions and then attach both to the same 'a' object.

But I really need to do that.

"Faço um monte de coisa..." was written in portuguese, sorry about it. It means "Do a lot of things"

Yes, the solution you propose is good, but I can't do everything in same session.

I really need to get 'b1' in diferrent sessions. I really need to close them twice.

Do you have another idea?

Thank you.

Cristiano


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 27, 2009 2:09 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
What about the cascade? Do you have one from A to B? If so, do you really need it? That is the only simple solution I can think of. Hibernate doesn't allow you to use more than one instance of the same entity in a session. So you'll need to figure out a way to make that happen.


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.