-->
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: Problem with detached objects
PostPosted: Sun Jan 25, 2009 9:14 am 
Newbie

Joined: Sun Jan 25, 2009 8:53 am
Posts: 1
Hello,

I'm writing a web application using Hibernate (JPA) + Wicket + Spring for my university project, and in certain situation i have detached entity passed to persist exception.

The object i try to persist has colletion of elements (One-To-Many relationship) of which every single one has association to another domain object (Many-To-One relationship) - Examination has collection of Samples, and every Sample is connected with exactly one Patient.

The form for registering new Examinations allows user to insert information about Sample and it's Patient, or load Patient's information from database if he was registered earlier, then add Sample to Examination and either add more samples or register (save to database) an Examination. Everything goes smoothly as long as i keep creating new Patients for every Sample, but when i try to bind a fresh Sample object with Patient loaded from the database i get mentioned exception when trying to persist Examination object.

I walked around this problem using entityManager.merge instead of entityManager.Persist for saving Examination objects, but it keeps bothering me, because I'm not sure if this is neat and safe solution.

I'd be very grateful, if you could suggest me some more elegant solution if there exists one (and as I'm just a begginer in this area, I'm pretty sure it does).

Best regards,
Michal Olejnik


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 25, 2009 4:51 pm 
Beginner
Beginner

Joined: Sat Oct 04, 2003 7:00 am
Posts: 26
Location: Roma,IT
Using merge is good solution, I think.

_________________
Filippo De Luca
Developer at WURFL-Pro


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 26, 2009 4:57 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
The persist-operation is to be used, when saving new objects, merge updates existing ones. If I understand your case, you are not invoking em.persist on an existing object but on an object which has an association to an already persistent object, right?

Maybe you are using cascade on persist-style, which would be the reason why the exception occurs. In this case, you should either disable cascade or use merge.

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


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.