• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Entity manager contains() method

 
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your answer Klaus.
 
reply
    Bookmark Topic Watch Topic
  • New Topic