public void test() {
removeAll(LazyEmployee.class);
em.getTransaction().begin();
for (int i = 0; i < NUMBER_OF_INSTANCES ; ++i) {
LazyEmployee e = createLazyEmployee(i);
em.persist(e);
}
em.getTransaction().commit();
em.clear();
em.getTransaction().begin();
Query query = em.createQuery("select e from LazyEmployee e where e.id = :id");
for (int i = 0; i < NUMBER_OF_INSTANCES ; ++i) {
query.setParameter("id", i);
LazyEmployee e = (LazyEmployee)query.getSingleResult();
int id = e.getId();
int magic = e.getMagic();
// name and age are lazily loaded
final int age;
final String name;
if (0 == i%2) {
// age and name are loaded separately because age has no load fetch group
age = e.getAge();
name = e.getName();
} else {
// age and name are loaded together because name's load fetch group includes age
name = e.getName();
age = e.getAge();
}
String result = new String("Lazy Employee " + id + " magic: " + magic + " age: " + age + " name: " + name);
// System.out.println(result);
}
em.getTransaction().commit();