Close

JPA - EntityManager.getReference Examples

Code Snippets JPA JAVA EE 

    private static void loadEntity(EntityManagerFactory emf) {
System.out.println("-- loading employee by id --");
EntityManager em = emf.createEntityManager();
System.out.println("calling getReference().");
Employee employee = em.getReference(Employee.class, 1);
System.out.println("getReference() already called");
System.out.println("loaded employee name: " + employee.getName());
em.close();
}
Original Post




    private static Employee getEntityReference(EntityManagerFactory emf) {
EntityManager em = emf.createEntityManager();
Employee employee = em.getReference(Employee.class, 1);
em.close();
return employee;
}
Original Post




    private static void updateByReference(EntityManagerFactory emf) {
System.out.println("-- updating employee by reference --");
EntityManager em = emf.createEntityManager();
Employee employee = em.getReference(Employee.class, 1);
em.getTransaction().begin();
System.out.println("-- setting new department --");
employee.setDepartment("IT");
System.out.println("-- commit --");
em.getTransaction().commit();
em.close();
}
Original Post




    private static void persistEmployee(EntityManagerFactory emf) {
System.out.println("-- persisting employee --");
Employee employee = new Employee();
employee.setName("Joe");
EntityManager em = emf.createEntityManager();
Task task = em.getReference(Task.class, 1);
employee.setTask(task);
em.getTransaction().begin();
em.persist(employee);
em.getTransaction().commit();
em.close();
System.out.println("Employee persisted: " + employee);
}
Original Post
    private static void persistEmployees(EntityManagerFactory emf) {
System.out.println("-- persisting employees --");

EntityManager em = emf.createEntityManager();
Department department = em.getReference(Department.class, 1);

Employee employee1 = new Employee();
employee1.setName("Joe");
employee1.setDepartment(department);

Employee employee2 = new Employee();
employee2.setName("Jackie");
employee2.setDepartment(department);

em.getTransaction().begin();
em.persist(employee1);
em.persist(employee2);
em.getTransaction().commit();
em.close();
System.out.println("Employee persisted: " + employee1);
System.out.println("Employee persisted: " + employee2);
}




    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 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 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 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




See Also