-->
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: two persistence units and detached entities
PostPosted: Wed May 30, 2007 7:15 am 
Newbie

Joined: Tue May 29, 2007 6:23 pm
Posts: 11
Hi;

I have two persistence units each representing two different schemas. These schemas have the same tables. The aim is to gather default entity values from the first persistence unit, and store the persistent entities on the second persistence unit. Here is how i try to mange this purpose:

EntityManagerFactory factory = Persistence.createEntityManagerFactory("firstpersistenceunit");
EntityManager em = factory.createEntityManager();
myEntity = em.find(MyEntity.class, 1);
em.close();
factory.close();
//myEntity.setId(0);
myentity.setName("bla bla");
EntityManagerFactory factory = Persistence.createEntityManagerFactory("secondpersistenceunit");
EntityManager em = factory.createEntityManager();
em.persist(myEntity);
em.close();
factory.close();

I can manage to persist the entity in the second schema, but if I uncomment the line setting the entity id as 0, I get the following error saying trying to persist a detached entity, which makes me confused. Why thus the object become detached when I set its id to 0 and can be persisted without any problem if I do not ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 30, 2007 12:06 pm 
Newbie

Joined: Tue May 29, 2007 12:33 am
Posts: 13
That may have to do with the unsaved value for the persistence id of your object. The ejb 3 spec does not specify explicitly how a persistence manager differentiates a new/transient entity from a dettached one (may be provider specific), but hibernate does.

Look at the unsaved-value attribute of the id element:

http://www.hibernate.org/hib_docs/v3/reference/en/html/mapping.html#mapping-declaration-id

Indeed, entity managers are supposed to throw an exception when attempting to persist a dettached entity

Quote:
"If X is a detached object, the EntityExistsException may be thrown when the persist operation is invoked, or the EntityExistsException or another PersistenceException may be thrown at flush or commit time."


So, when not setting the Id to 0, what is the value of this property right before persisting the entity through the second entity manager? Is it 1? null?

Hope this helps a little,

Regards,


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 31, 2007 2:39 am 
Newbie

Joined: Tue May 29, 2007 6:23 pm
Posts: 11
kakaroto wrote:
So, when not setting the Id to 0, what is the value of this property right before persisting the entity through the second entity manager? Is it 1? null?

Hope this helps a little,

Regards,


The id is 1 at that time, since I assigned 1 to the id's of entities in the first persistence unit. The stranger thing is when I add an element to a collection (actually an ArrayList) of the entity (MyEntity), if I do not set its id as 0, then I get "PersistentObjectException: detached entity passed to persist" exception. So it seems I have to give a not null id to the root, and give 0 to ids to each children in order to persist each one. Although I could not get the idea behind...


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.