-->
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.  [ 5 posts ] 
Author Message
 Post subject: Detached Entity Passed to Persist errors
PostPosted: Thu Jan 29, 2009 1:34 am 
Beginner
Beginner

Joined: Thu Jan 29, 2009 1:24 am
Posts: 20
Hello everyone.

I'm using Hibernate 3.2.6.GA with Hibernate-EntityManager 3.3.1.GA and Hibernate-C3P0 3.3.1.GA (maven deps). Hibernate is hooked up through Spring 2.5. All of my persistent objects are written in JPA with the DAOs written with Spring JPA. The database behind all of this is MySQL 5 and i'm using C3P0 to manage the connection pool.

Here's my scenario. I have two classes; let's call them Model and Brand. I create a new Brand object and persist it to the database. I then create a new Model object and assign the recently persisted Brand object as part of its member variable. In the Model class Brand is linked using the following annotations.

Code:
@OneToOne(cascade = CascadeType.ALL)
@PrimaryKeyJoinColumn


Now when I try to persist the Model object I get the following error from Hibernate.

Code:
org.hibernate.PersistentObjectException: detached entity passed to persist: Brand


Each call to persist occurs within its own transaction as the Dao classes are written as @Transactional. Can anyone explain why this is happening? This is a pretty big problem for me if I can't link classes together.

Thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 29, 2009 5:31 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
You use @OneToOne(cascade = CascadeType.ALL), so you tell hibernate to also persist the brand, when persisting a model. You can either disable cascading or use merge(brand) before persisting your model.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 29, 2009 2:52 pm 
Beginner
Beginner

Joined: Thu Jan 29, 2009 1:24 am
Posts: 20
Ok. I tried using merge(brand) before the persist(model) and it doesn't seem to have made any effect.

I'm fairly new to the whole hibernate/jpa setup. I was expecting that hibernate would realize the brand was already in the database and just wouldn't do anything. Only if the brand was clearly not in the db would it get persisted. I was under the impression you get all of this for free (after all isn't that the point of a persistence tech, to take care of tedious book keeping). Maybe i'm expecting too much from hibernate.

Is there an annotation I can give the brand member variable in model that will effectively do what I want?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 31, 2009 8:08 pm 
Newbie

Joined: Fri Jan 02, 2009 4:26 pm
Posts: 5
You don't need to call persist. Do one of the following:

Add (unpersisted) brand to model and merge model. Since your CascadeType is set to all, it will insert both.

Or...use the brand RETURNED from merge to add to model. Then persist on model should work.

Merge will help w/ DetachedEntity issues because it will figure out from the ID whether to update or insert


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 31, 2009 9:58 pm 
Beginner
Beginner

Joined: Thu Jan 29, 2009 1:24 am
Posts: 20
cretz wrote:
You don't need to call persist. Do one of the following:

Add (unpersisted) brand to model and merge model. Since your CascadeType is set to all, it will insert both.

Or...use the brand RETURNED from merge to add to model. Then persist on model should work.

Merge will help w/ DetachedEntity issues because it will figure out from the ID whether to update or insert


Adding an unpersisted model and brand works no problem.

The issue here is that hibernate is behind a web service and the web service interacts with clients. So a common situation is a client wishes to create a new model. The client pulls the brand from the web service, adds it to the new model object and then attempts to push the new model to the web service. Then the web service attempts to add the model to the database and since the brand is already inserted we get an error. I can perform merges or pull the data again before I attempt to insert the model but I was hoping to avoid having Dao's rely on other Dao's. Maybe that is the only way though.


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