JPA - EntityManager.contains Examples

Code Snippets JPA JAVA EE 

    private static void logPersistentStatus(String msg, Employee employee, EntityManager em) {
System.out.printf("-- %s --%n", msg);
System.out.println("employee#id: " + employee.getId());
System.out.println("EntityManager#contains(): " + em.contains(employee));
}
Original Post




    private static void persistEntity() {
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();
}
Original Post




Calling contains() after EntityManager.close() will throw an exception.

    private static void persistEntity() {
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));
}
Original Post




    private static void findEntity() {
EntityManager em = entityManagerFactory.createEntityManager();
Employee employee = em.find(Employee.class, 1);
System.out.println("after find(): " + em.contains(employee));
em.close();
}
Original Post
    private static void findByGetReference() {
EntityManager em = entityManagerFactory.createEntityManager();
Employee employee = em.getReference(Employee.class, 1);
System.out.println("after find(): " + em.contains(employee));
em.close();
}
Original Post




    private static void findAndRemoveEntity() {
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();
}
Original Post




    private static void findByReferenceAndRemoveEntity() {
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();
}
Original Post
    private static void findAndDetachEntity() {
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();
}
Original Post




    private static void findByReferenceAndDetachEntity() {
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();
}
Original Post




    private static void findDetachAndMergeEntity() {
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();
}
Original Post
    private static void findByRefDetachAndMerge() {
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();
}
Original Post




    private static Employee checkAndInitialize(Employee employee) {
PersistenceUtil pu = entityManagerFactory.getPersistenceUnitUtil();
System.out.println(" Employee loaded: " + pu.isLoaded(employee));
System.out.println("Employee.phones loaded: " + pu.isLoaded(employee, "phones"));
if (!pu.isLoaded(employee) //entity might've been retrieved via getReference
|| !pu.isLoaded(employee, "phones")//phones is a lazy relation
) {
System.out.println("initializing employee");
boolean detached = !entityManager.contains(employee);
System.out.println("is employee detached: " + detached);
if (detached) {
System.out.println("merging employee");
employee = entityManager.merge(employee);
}
//this will load/initialize employee entity
employee.getName();
employee.getDepartment();
//this will load lazy phones field
employee.getPhones().size();
entityManager.detach(employee);
//now employee is fully initialized
System.out.println("employee initialized");
}
return employee;
}
Original Post




See Also