-->
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.  [ 6 posts ] 
Author Message
 Post subject: Detatched entities for Application Entity Manager
PostPosted: Thu Apr 05, 2007 9:33 am 
Senior
Senior

Joined: Mon Jul 24, 2006 8:43 am
Posts: 160
Hi,
When does an attatched Entity become detatched (if ever) when using an Application managed (i.e. not injected from an Application server?

My understanding is an Application Entity Manager always uses an extended Persistence Context so the attatched Entity nevers becomes an detatched?

However I wrote a little bit of test code for my address entity:
Code:
// begin an entity transaction
em.getTransaction().begin();
Address addressRead = (Address)(em.createQuery("from Address address where address.id = 15");
System.out.println("Attatched:" +  em.contains(addressRead));
em.getTransaction().commit();
System.out.println("Attatched:" +  em.contains(addressRead));

I would have expected this to output: true, true i.e. always attatched but it outputs true, false. It becomes detatched after the the transaction is committed. But the EntityManager uses an extended Persistence Context so this should not happen - so why does it?
Regards


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 12, 2007 5:08 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
It should behave as you expect.

Which container are you using?

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 13, 2007 12:08 pm 
Senior
Senior

Joined: Mon Jul 24, 2006 8:43 am
Posts: 160
emmanuel wrote:
It should behave as you expect.

Which container are you using?

I am using an Application Managed Entity Manager i.e. no container I am running in standalone mode.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 13, 2007 2:45 pm 
Newbie

Joined: Wed Apr 11, 2007 5:54 pm
Posts: 4
From the JPA spec [3.3 Persistence Context Lifetime]
Quote:
An EntityManager with an extended persistence context maintains its references to the entity objects
after a transaction has committed. Those objects remain managed by the EntityManager, and they may
be updated as managed objects between transactions.

So you should be seeing true, true if you're really using application-managed entity manager.

As for your question about when entity becomes detached, JPA spec [3.2.4 Detached Entities]
Quote:
A detached entity may result from transaction commit if a transaction-scoped container-managed entity
manager is used (see section 3.3); from transaction rollback (see section 3.3.2); from clearing the persistence
context; from closing an entity manager; and from serializing an entity or otherwise passing an
entity by value—e.g., to a separate application tier, through a remote interface, etc.

The most common cases are after EntityManager.clear() or EntityManager.close().


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 16, 2007 2:30 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
breako wrote:
emmanuel wrote:
It should behave as you expect.

Which container are you using?

I am using an Application Managed Entity Manager i.e. no container I am running in standalone mode.


Application managed does not mean you are not getting the EM from a container, it just says that the EM lifecycle is your responsibility.
do you use Persistence.createEMF or do you retrieve the EMF from JNDI?

For standalone mode (Persistence.createEMF), I have checked and I have a unit test that behave as expected.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 28, 2007 8:06 am 
Senior
Senior

Joined: Mon Jul 24, 2006 8:43 am
Posts: 160
Hi,
I do
Persistence.createEMF().
I am using the following versions:

Annotations: Version: 3.2.0 CR1, 13.05.2006
Core: version 3.2 cr2, May 5, 2006
EntityManager: Version: 3.2.0.CR1, 13.05.2006

I will update to latest GA version and see if that happens.


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