JPA和Hibernate代理行为

时间:2012-11-14 15:48:09

标签: hibernate jpa jpa-2.0 hibernate-4.x

我试着观察下面的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。我可能在这里错过了一些东西,那么如何才能做到正确?非常感谢一个简单但具体的例子!

@EDIT

根据@kostja的答案,我调整了代码并在下面的SE模式下调试了代码,LazyInitializationException既不会产生boss property也不会代理@kostja。还有其他提示吗?

code window

debug window

@EDIT 2

最后,我确认proxied boss property的答案无疑是伟大的。

我在EE模式下测试过,所以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

//输出:

{{1}}

另外,可以参考Hibernate docs中的20.1.4. Initializing collections and proxies

1 个答案:

答案 0 :(得分:7)

这是预期的JPA行为。您的查询中的实体没有理由被代理 - 这是查询的常规结果。但是,该实体的boss属性应该是代理。它不会告诉我是否 - 当你对托管实体的延迟加载属性执行任何操作时,它将触发提取。

所以你应该访问交易之外的boss属性。如果尚未提取,则会获得LazyInitializationException

如何处理它取决于EntityManagerPersistenceContext的种类。

  • 仅在JPA 2.0起作用 - 调用em.detach(loadedEmployee)然后访问boss属性。

对于JPA 1:

  • 如果您在Java EE环境中,请使用@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)标记该方法以暂停该事务。

  • 在具有用户交易的SE环境中,在访问transaction.commit()属性之前调用boss

  • 如果使用超出交易的EXTENDED PersistenceContext,请致电em.clear()

EIDT:我认为你没有得到异常的原因是FetchType.LAZY只是JPA提供者的提示,因此无法保证懒惰地加载该属性。与此相反,FetchType.EAGER保证了渴望获取。我想,你的JPA提供商选择热切地加载。

我已经复制了这个例子,虽然有点不同,我可以在日志语句中重复获得LazyInitializationException。该测试是在JBoss 7.1.1上运行的Arquillian测试,JPA 2.0基于Hibernate 4.0.1:

@RunWith(Arquillian.class)
public class CircularEmployeeTest {
    @Deployment
    public static Archive<?> createTestArchive() {
        return ShrinkWrap
                .create(WebArchive.class, "test.war")
                .addClasses(Employee.class, Resources.class)
                .addAsResource("META-INF/persistence.xml",
                        "META-INF/persistence.xml")
                .addAsResource("testSeeds/2CircularEmployees.sql", "import.sql")
                .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
    }

    @PersistenceContext
    EntityManager em;

    @Inject
    UserTransaction tx;

    @Inject
    Logger log;

    @Test
    @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
    public void testConfirmLazyLoading() throws Exception {
        String query = "SELECT e FROM Employee e WHERE e.id = 1";

        tx.begin();
        Employee employee = em.createQuery(query,
                Employee.class).getSingleResult();
        tx.commit();
        log.info("retrieving the boss: {}", employee.getBoss());
    }
}