JPA - EntityManger.refresh Examples

Code Snippets JPA JAVA EE 

    private static void displayEntities(EntityManagerFactory emf) {
//loading all entities
EntityManager em = emf.createEntityManager();
Query query = em.createQuery("Select e from Employee e");
List<Employee> list = (List<Employee>) query.getResultList();
for (Employee employee : list) {
//sending to ui for display
displayEntity(employee);
print("refreshing employee", employee);
//now refreshing entity before doing something with it
em.refresh(employee);
print("after refreshing employee", employee);
}
}

private static void displayEntity(Employee employee) {
System.out.println("-- displaying employee --");
System.out.println(employee);
employee.setDepartment("Sales");
print("employee modified in displayEntity()", employee);
}
Original Post




See Also