JPA - EntityManager.remove Examples

Code Snippets JPA JAVA EE 

    private static void removeEntity(EntityManagerFactory emf, int id) {
EntityManager em = emf.createEntityManager();
Employee employee = em.find(Employee.class, id);
System.out.println("-- removing entity --");
printObject(employee);

em.getTransaction().begin();
em.remove(employee);
em.getTransaction().commit();
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




See Also