aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes Check to see if an entity is managed by the persistence context Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Check to see if an entity is managed by the persistence context" Watch "Check to see if an entity is managed by the persistence context" New topic
Author

Check to see if an entity is managed by the persistence context

Deniz Atak
Ranch Hand

Joined: Jun 29, 2011
Posts: 30
Hi I have a code where I try to check if an entity is managed by the persistence context. If it does it will perform an action, if it doesn't it will perform another action. My code is:



I might have a logical mistake in trying first find and then contains method.. but I don't know.

Thanks in advance for your help.
lokesh sree
Ranch Hand

Joined: Oct 27, 2009
Posts: 100


In case of transaction scoped persistence context:
An entity will be loaded into the persistence context, if it is fetched in the scope of a transaction.
And it will get detached , the context gets closed, when the transaction ends.

So, I believe that, if the code block that you have posted is running in the scope of a tx, the contains() call that is made should always return true for any entity.

Lokesh
( SCBCD 5, CCENT, SCJP 5 )
Deniz Atak
Ranch Hand

Joined: Jun 29, 2011
Posts: 30
Sorry for the late answer. Thanks for your reply.

Your explanation is right if the entity exists. But I am trying to handle the case when it doesn't exist. It works fine in the true case (=when .contains returns true) but doesn't catch the null pointer exception.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Check to see if an entity is managed by the persistence context