This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Entity manager contains() method

 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 918
IntelliJ IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your answer Klaus.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic