i tried to observe JPA2 / Hibernate4 proxy behavior below,
// Circular entity with lazy loading:
@Entity
public class Employee {
@Id@Generated
int id;
String name;
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
Employee boss;
public String toString() {
return id + "|" + name + "|" + boss;
}
//getters and setters ...
}
// Persist entities:
// Outer entity:
Employee employee = new Employee();
employee.setName("engineer");
// Inner entity:
Employee boss = new Employee();
boss.setName("manager");
employee.setBoss(boss);
entityTransaction.begin();
entityManager.persist(employee);
entityTransaction.commit();
System.out.println(employee);
// Output:
Hibernate: insert into Employee (id, boss_id, name) values (default, ?, ?)
Hibernate: insert into Employee (id, boss_id, name) values (default, ?, ?)
2|engineer|1|manager|null
// Load the outer entity:
String queryString = "select e from Employee e where e.id=" + employee.getId();
Query query = entityManager.createQuery(queryString);
Object loadedEmployee = query.getSingleResult();
System.out.println(loadedEmployee.getClass().getSimpleName());
// Output:
Hibernate: select employee0_.id as id2_, employee0_.boss_id as boss3_2_, employee0_.name as name2_ from Employee employee0_ where employee0_.id=2 limit ?
Employee
To my surprise the loaded outer entity above is still the plain one, but i'd expected it to be Hibernate proxy
resulting from lazy loading
. I could have missed something here, so how to get it right? A simple yet concrete example is much appreciated!
@EDIT
According to the answer from @kostja
i adapted the code and debugged it in SE mode below, neither could LazyInitializationException
be produced, nor was boss property
proxied. Any further hints?
@EDIT 2
Finally, i'd confirm that the answer from @kostja
is undoubtly great.
I tested in EE mode, so the proxied boss property
was observed below,
// LazyInitializationException
thrown:
public Employee retrieve(int id) {
Employee employee = entityManager.find(Employee.class, id);
// access to the proxied boss property outside of persistence/transaction ctx
Employee boss = employee.getBoss();
System.out.println(boss instanceof HibernateProxy);
System.out.println(boss.getClass().getSimpleName());
return boss;
}
// Green light after put Spring Tx
in place:
@Transactional
public Employee retrieve(int id) ...
// Output:
true
Employee_$$_javassist_0
Also, one can refer to 20.1.4. Initializing collections and proxies from Hibernate docs.