我试图观察下面的 JPA2 / Hibernate4 代理行为,
// 带有延迟加载的循环实体:
@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 ...
}
// 持久化实体:
// 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);
// 输出:
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
// 加载外部实体:
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());
// 输出:
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
令我惊讶的是,上面加载的外部实体仍然是普通实体,但我预计它是Hibernate proxy
由lazy loading
. 我可能在这里错过了一些东西,那么如何让它正确呢?非常感谢一个简单而具体的例子!
@编辑
根据我的回答,@kostja
我修改了代码并在下面的 SE 模式下对其进行了调试,既不能LazyInitializationException
生成,也不能被boss property
代理。任何进一步的提示?
@编辑 2
最后,我要确认来自的答案@kostja
无疑是很棒的。
我在EE模式下进行了测试,所以在proxied boss property
下面观察到,
//LazyInitializationException
抛出:
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;
}
Spring Tx
//就位后绿灯亮:
@Transactional
public Employee retrieve(int id) ...
// 输出:
true
Employee_$$_javassist_0