It's not a secret anymore!*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Entity manager contains() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Entity manager contains() method" Watch "Entity manager contains() method" New topic
Author

Entity manager contains() method

Mihai Radulescu
Ranch Hand

Joined: Sep 18, 2003
Posts: 916

Hi

As far as I understand the contains method from the EntityManager is used to prove if a given instance is or not managed by a given context.
What this method does not do is to prove if a entity is persisted (exist is the database).
The following example confuse me :


The JPA is transactional.
In this example the myEntity gets managed (with the persist method call) and then persisted (when the transaction ends). When the "em.contains(myEntity)" occurs the MyEntity is still managed thats why the em.contains(myEntity) returns true.



In this example the myEntity is managed, persisted and after this is goes unmanaged(when the method a ends). In the method b the myEntity is unmanaged even if its content exist already in the database. That is the reason why the em.contains(myEntity) returns false.

Am I correct ?

Regards,
Mihai


SCJP, SCJD, SCWCD, OCPJBCD
Mihai Radulescu
Ranch Hand

Joined: Sep 18, 2003
Posts: 916

Mihai Radulescu wrote:Hi

As far as I understand the contains method from the EntityManager is used to prove if a given instance is or not managed by a given context.
What this method does not do is to prove if a entity is persisted (exist is the database).
The following example confuse me :


The JPA is transactional.
In this example the myEntity gets managed (with the persist method call) and then persisted (when the transaction ends). When the "em.contains(myEntity)" occurs the MyEntity is still managed thats why the em.contains(myEntity) returns true.



In this example the myEntity is managed, persisted and after this is goes unmanaged(when the method a ends). In the method b the myEntity is unmanaged even if its content exist already in the database. That is the reason why the em.contains(myEntity) returns false.

Am I correct ?

Regards,
Mihai
Klaus Schultz
Greenhorn

Joined: Aug 29, 2009
Posts: 15
In your example the client is not shown, method b() may have the same transaction as method a()
if they are called from the client with the same transactional context.

If for method b() the container has started a new transaction, the entity manager has a new persistence context, which you query in line 12 with contains(myEntity). You didn't code a find() or something else to populate the persistence context.


Klaus
SCBCD 93%
Mihai Radulescu
Ranch Hand

Joined: Sep 18, 2003
Posts: 916

Hi Klaus

Thanks for your answer Klaus.
Your arguments are logic and they confirm my presumption.
The call for method a and b comes from different transactions and if they will come from the same one the I will use the @TransactionAttribute(TransactionAttributeType.REQUIRED_NEW) " to create a new transaction and attach the persistence context to it.

The code is with purpose like this. I try to prove (my self) that the contains proves the contains method functionality.
This code snippets shows that the contains prove if a entity is in a certain context and it does not prove if the entity exist in the database.

What do you think ?

Regards,
Mihai
Klaus Schultz
Greenhorn

Joined: Aug 29, 2009
Posts: 15
Hi,

>>This code snippets shows that the contains prove if a entity is in a certain context and it does not prove if >>the entity exist in the database.
Sure. To quote the javadoc of the contains method:
"Check if the instance belongs to the current persistence context."
That's a quite clear statement. If you want to know if the entity exists in the database, you must do something like find().
Mihai Radulescu
Ranch Hand

Joined: Sep 18, 2003
Posts: 916

Thanks for your answer Klaus.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Entity manager contains() method
 
Similar Threads
Answers of Sun's Free Proficiency Assessment
find and persist in the same trans.
CMT: Container Management Transaction
Persit method strange behaviour
Understanding Persistence Context Propagation