Java Tutorial/JPA/Lazy Eager — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 05:18, 1 июня 2010
Detachment Triggered Lazy Loading
File: Main.java
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
public class Main {
static EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPAService");
static EntityManager em = emf.createEntityManager();
public static void main(String[] a) throws Exception {
em.getTransaction().begin();
Student emp1 = new Student();
emp1.setId(1);
em.persist(emp1);
Query query = em.createQuery("SELECT e FROM Student e");
List<Student> list = (List<Student>) query.getResultList();
System.out.println(list);
List<Student> emps = em.createQuery("SELECT e FROM Student e").getResultList();
for (Student emp : emps) {
if (emp.getDepartment() != null) {
emp.getDepartment().getName();
}
}
em.getTransaction().rumit();
em.close();
emf.close();
Helper.checkData();
}
}
Detachment With Eager Loading
File: Main.java
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
public class Main {
static EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPAService");
static EntityManager em = emf.createEntityManager();
public static void main(String[] a) throws Exception {
em.getTransaction().begin();
Student emp1 = new Student();
emp1.setId(1);
em.persist(emp1);
Query query = em.createQuery("SELECT e FROM Student e");
List<Student> list = (List<Student>) query.getResultList();
System.out.println(list);
List<Student> emps = em.createQuery("SELECT e FROM Student e").getResultList();
for (Student emp : emps) {
if (emp.getDepartment() != null) {
emp.getDepartment().getName();
}
}
em.getTransaction().rumit();
em.close();
emf.close();
Helper.checkData();
}
}