-->
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: Persist an object with detached objects
PostPosted: Thu Jun 22, 2006 11:06 am 
Beginner
Beginner

Joined: Wed Jun 07, 2006 6:11 am
Posts: 20
Hi!

How can i persist an transient object with detached objects?

That means, I've got an transient address object which should be filled with the detached objects City and Zipcode.

With the method merge it works, but I do not get the ID of the address object, but it is written to the database (with the method persist I'll get an ID). The method refresh fails, because the address object does not have an ID. Why? How can I persist this address object correctly, thus I get an ID.

Thanks!
Christian


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 22, 2006 7:56 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
You must use persist, save or saveOrUpdate. Don't worry about the nested objects being detached: detached objects still have the correct IDs, and that's all you need to save the references correctly.

If you want to re-attach the detached nested objects, call refresh on the address object after persisting it.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 23, 2006 2:04 am 
Beginner
Beginner

Joined: Wed Jun 07, 2006 6:11 am
Posts: 20
Hibernate version: 3.2.0.cr2
Hibernate Annotations: 3.2.0.CR1
Hibernate Entity Manager: 3.2.0.CR1
JBoss version: 4.0.4.GA
EJB3 version: EJB-3.0 RC8-FD
Name and version of the database you are using: Ingres 3.0.105

The problem is, that I can not use detached objects in the persist method, because this will cause a org.hibernate.PersistentObjectException: detached entity passed to persist. And I can only call the methods persist and merge, because I'm using an EntityManager.

Here is my code example:

Code:
City city = new City();
city.setName("cityName");
Zipcode code = new Zipcode();
code.setCode("myCode");

em.persist(city);
em.persist(code);


Now I want to create a new Address (zipcode and city must not be null in the database!).

Code:
Address ad = new Address();
ad.setCity(city); // detached object city
ad.setZipcode(code); // detached object code


Code:
em.persist(ad);
// this will cause the exception and adds no data to the database

or

Code:
em.merge(ad);
// this will work (adds data to the database), but the address object will not get an ID (getId() == null)!


Top
 Profile  
 
 Post subject: Very similar problem here
PostPosted: Wed Jun 28, 2006 11:06 am 
Newbie

Joined: Mon Sep 26, 2005 11:10 am
Posts: 5
I am moving from straight Hibernate to the HibernateEntityManager.

I have a City, State, and ZipCode which were saved in another session, then I create a new Address with the City, State, and ZipCode. (In my case, the City, State, and ZipCode do have proper id fields). When I go to save the address using em.persist() I get:

org.hibernate.PersistentObjectException: detached entity passed to ...

I did this all the time with stright hibernate. My hbm.xml files have cascade=all on the relationships to the dependant objects.

I am using the same versions mentioned above, except I'm not inside JBoss at this point, just trying to get the JUnits working.

Any suggestions would be greatly appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 28, 2006 5:19 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
You can't have done that. Session.persist() accepts only transient objects, not detached ones. The exception is correct, your code is wrong.

It is polite to start a new topic to ask a new question.

_________________
Code tags are your friend. Know them and use them.


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.