JPA - Getting Entity's Managed State with EntityManager.contains()

[Updated: Dec 6, 2017, Created: Dec 5, 2017]

EntityManager.contains(entityInstance) can be used to find out if the entity instance is a managed entity instance belonging to the current persistence context.

Examples

The Entity

@Entity
public class Employee {
  @Id
  @GeneratedValue
  private Integer id;
  private String name;
  private String department;
    .............
}

Using EntityManager.contains() method

Before and after Persisting

If the entity instance is new, contains() will return false. If the persist method has been called on the entity or the persist operation has been cascaded to it, this method will return true.

   EntityManager em = entityManagerFactory.createEntityManager();
   Employee employee = new Employee("Sara", "Admin");
   System.out.println("New entity: " + em.contains(employee));
   em.getTransaction().begin();
   em.persist(employee);
   System.out.println("After EntityManager.persist() : " + em.contains(employee));
   em.getTransaction().commit();
   System.out.println("After commit: " + em.contains(employee));
   em.close();
New entity: false
After EntityManager.persist() : true
After commit: true

After EntityManager.close()

   EntityManager em = entityManagerFactory.createEntityManager();
   Employee employee = new Employee("Joe", "IT");
   em.getTransaction().begin();
   em.persist(employee);
   em.getTransaction().commit();
   em.close();
   System.out.println("After EntityManager.close(): " + em.contains(employee));
java.lang.IllegalStateException: Session/EntityManager is closed
at org.hibernate.internal.AbstractSharedSessionContract.checkOpen(AbstractSharedSessionContract.java:337)
at org.hibernate.engine.spi.SharedSessionContractImplementor.checkOpen(SharedSessionContractImplementor.java:135)
at org.hibernate.internal.SessionImpl.contains(SessionImpl.java:1983)
at com.logicbig.example.ContainsExample2.persistEntity(ContainsExample2.java:26)
at com.logicbig.example.ContainsExample2.main(ContainsExample2.java:13)

After Retrieving from database

This method returns true if the entity has been retrieved from the database by find() or by JPQL query or by getReference().

Using find():

   EntityManager em = entityManagerFactory.createEntityManager();
   Employee employee = em.find(Employee.class, 1);
   System.out.println("after find(): " + em.contains(employee));
   em.close();
after find(): true

Using getReference():

   EntityManager em = entityManagerFactory.createEntityManager();
   Employee employee = em.getReference(Employee.class, 1);
   System.out.println("after find(): " + em.contains(employee));
   em.close();
after find(): true

In above example, contains() method will still not load the proxy entity. Note that getReference() loads uninitialized proxy instance of the entity instead of full initialized instance (tutorial here).

After Removing

Returns false if entity has been removed:

   EntityManager em = entityManagerFactory.createEntityManager();
   Employee employee = em.find(Employee.class, 1);
   System.out.println("after find(): " + em.contains(employee));
   em.remove(employee);
   System.out.println("after remove(): " + em.contains(employee));
   em.close();
after find(): true
after remove(): false
   EntityManager em = entityManagerFactory.createEntityManager();
   Employee employee = em.getReference(Employee.class, 1);
   System.out.println("after getReference(): " + em.contains(employee));
   em.remove(employee);
   System.out.println("after remove(): " + em.contains(employee));
   em.close();
after getReference(): true
after remove(): false

After detaching

Return false if entity has been detached

   EntityManager em = entityManagerFactory.createEntityManager();
   Employee employee = em.find(Employee.class, 1);
   System.out.println("after find(): " + em.contains(employee));
   em.detach(employee);
   System.out.println("after detach(): " + em.contains(employee));
   em.close();
after find(): true
after detach(): false
   EntityManager em = entityManagerFactory.createEntityManager();
   Employee employee = em.getReference(Employee.class, 1);
   System.out.println("after getReference(): " + em.contains(employee));
   em.detach(employee);
   System.out.println("after detach(): " + em.contains(employee));
   em.close();
after getReference(): true
after detach(): false

After Merging

contains() method returns true if a detached entity has been merged.

   EntityManager em = entityManagerFactory.createEntityManager();
   Employee employee = em.find(Employee.class, 1);
   System.out.println("after find(): " + em.contains(employee));
   em.detach(employee);
   System.out.println("after detach(): " + em.contains(employee));
   employee = em.merge(employee);
   System.out.println("after merging(): " + em.contains(employee));
   em.close();
after find(): true
after detach(): false
after merging(): true
   EntityManager em = entityManagerFactory.createEntityManager();
   Employee employee = em.getReference(Employee.class, 1);
   System.out.println("after getReference(): " + em.contains(employee));
   em.detach(employee);
   System.out.println("after detach(): " + em.contains(employee));
   employee = em.merge(employee);
   System.out.println("after merge(): " + em.contains(employee));
   em.close();
after getReference(): true
after detach(): false
after merge(): true

Note that the effect of the cascading of persist, merge, remove, or detach is immediately visible to the contains method.

Example Project

Dependencies and Technologies Used :

  • h2 1.4.196: H2 Database Engine.
  • hibernate-core 5.2.10.Final: The core O/RM functionality as provided by Hibernate.
  • JDK 1.8
  • Maven 3.3.9

EntityManager.contains() Example Select All Download
  • entity-manager-contains-example
    • src
      • main
        • java
          • com
            • logicbig
              • example
        • resources
          • META-INF

See Also