-->
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.  [ 6 posts ] 
Author Message
 Post subject: Parent Child question
PostPosted: Wed Mar 31, 2010 9:42 am 
Beginner
Beginner

Joined: Fri Jul 20, 2007 10:38 am
Posts: 49
Hello,

I have a parent-child of the same class. When I declare a persisted object to have a parent, I get a org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [class + objects' primary key here]. I don't have this issue when running this change as a unit-test.

If I create the child to have a parent and INSERT it, it is persisted.
So basically, changing a parent to a child doesn't work (it's still the same object-class).

The relation is declared as a <many-to-one name="parent".. /> -relation.
I've set insert and update to false, my cascade is set to none.
I've changed several attributes, but get the same results.

What am I missing?

Thanks!


Top
 Profile  
 
 Post subject: Re: Parent Child question
PostPosted: Wed Mar 31, 2010 9:55 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
How do you define the @Id property in your entity class?
Do you use a generated value or do you set the id manually?


Top
 Profile  
 
 Post subject: Re: Parent Child question
PostPosted: Wed Mar 31, 2010 10:03 am 
Beginner
Beginner

Joined: Fri Jul 20, 2007 10:38 am
Posts: 49
The ID is generated. What basically happens on the save is this:

[code]if (parentIsSet()) {
Parent parent = dao.getParent(parentId);
person.setParent(parent);
dao.saveOrUpdate(person);
[/code]


Top
 Profile  
 
 Post subject: Re: Parent Child question
PostPosted: Thu Apr 01, 2010 12:35 pm 
Beginner
Beginner

Joined: Fri Jul 20, 2007 10:38 am
Posts: 49
I've turned on the Hibernate.SQL to debug, it says:
Code:
def.AbstractSaveEventListener  - detached instance of: <class here>
def.DefaultSaveOrUpdateEventListener  - updating detached instance
def.DefaultSaveOrUpdateEventListener  - updating [<class here>#<id here>]


What I see is that that is correct, it should do an update, still it throws a:
Code:
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: <class here>#<id here>]
   at org.hibernate.engine.StatefulPersistenceContext.checkUniqueness(StatefulPersistenceContext.java:590)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:284)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:223)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:89)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)


I'm really stunned here. I have removed the cascade attribute from the hbm-file and also set it to none. None works. What to do?


Top
 Profile  
 
 Post subject: Re: Parent Child question
PostPosted: Fri Apr 02, 2010 4:35 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Why do you detach and re-attach instances in your unit of work ?


Top
 Profile  
 
 Post subject: Re: Parent Child question
PostPosted: Tue Apr 06, 2010 3:43 am 
Beginner
Beginner

Joined: Fri Jul 20, 2007 10:38 am
Posts: 49
I'm using Spring (1.2.8) and a web frontend, I guess the OpenSessionInViewFilter does this for me.
My transactions are configured on the dao's method saveOrUpdate (PROPAGATION_REQUIRED).


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