JPA - EntityManager.find 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 Employee loadEmployeeById(int id) {
System.out.println("-- loading employee by id --");
EntityManager em = emf.createEntityManager();
return em.find(Employee.class, id);
}
Original Post




    private static Person loadPersonById(long id) {
EntityManager em = emf.createEntityManager();
Person person = em.find(Person.class, id);
em.close();
return person;
}
Original Post




    private static Person loadPersonById(long id) {
EntityManager em = emf.createEntityManager();
Person person = em.find(Person.class, id);
em.close();
return person;
}
Original Post
    private static void updateByFinding(EntityManagerFactory emf) {
System.out.println("-- updating employee by finding --");
EntityManager em = emf.createEntityManager();
Employee employee = em.find(Employee.class, 1);
em.getTransaction().begin();
System.out.println("-- setting new name -- ");
employee.setName("Diana");
System.out.println("-- committing --");
em.getTransaction().commit();
em.close();
}
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 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 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 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 updateArticle() {
log("updating Article entity");
EntityManager em = entityManagerFactory.createEntityManager();
em.getTransaction().begin();
Article article = em.find(Article.class, 1L, LockModeType.PESSIMISTIC_WRITE);
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
article.setContent("updated content .. ");
log("committing in write thread.");
em.getTransaction().commit();
em.close();
log("Article updated", article);
}
Original Post
    private static void readArticle() {
log("before acquiring read lock");
EntityManager em = entityManagerFactory.createEntityManager();
em.getTransaction().begin();
Article article = em.find(Article.class, 1L, LockModeType.PESSIMISTIC_READ);
log("After acquiring read lock", article);
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
em.getTransaction().commit();
em.close();
log("Article after read commit", article);
}
Original Post




See Also