-->
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.  [ 2 posts ] 
Author Message
 Post subject: How to add exisitng object to OneToMany
PostPosted: Wed Jul 04, 2007 2:00 pm 
Regular
Regular

Joined: Wed May 02, 2007 2:42 pm
Posts: 101
I have Hotel which has a collection of Room (OneToMany - unidirectional).


in transaction A:
I save new Room (id=1)

in this stage room FK is null

In transaction B:
Code:
Room room = em.get(Room .class,new Long(1))
Hotel hotel = em.get(Hotel.class,new Long(1))
hotel .getRooms().add(room)



But, i got an exception:
Code:
Caused by: org.hibernate.PersistentObjectException: detached entity passed to persist: model.Room


How can i add exisiting Object to a unidirectional collection?

Thank you


Top
 Profile  
 
 Post subject: Sorry this code work
PostPosted: Wed Jul 04, 2007 5:55 pm 
Regular
Regular

Joined: Wed May 02, 2007 2:42 pm
Posts: 101
But,

If you send detach Room to transaction B and add it to the collection you will get an exception.

Hibernate think that it need to call to persist (i dont understand why).

If you will call explitity to mere on the collection it will be ok.

If you will put cascade mere on collection it will work.

If you put cascade.all on the collection you will get the same exception, because hibernate think it need to call to persist (dont understand why)


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