我在Eclipse SE中使用EclipseLink作为Java SE项目中的JPA提供程序。我已经正确配置了编织以允许延迟加载。与Hibernate(抛出LazyInitializationException)不同,EclipseLink可以获得LAZY关系的代理,即使是关闭了EntityManager也是如此。要运行此查询,它会从池中获取新连接。JPA + EclipseLink - 使用已关闭的EntityManager进行延迟加载
是否有一些设置禁用或更改此功能的行为?当我尝试访问一个未加载的属性时(例如Hibernate),我需要得到一个空值或一个异常。
实施例:
List<Customer> customers = entityManager.createQuery("from Customer c", Customer.class).getResultList();
entityManager.close(); // Closing the EntityManager
for (Customer customer: customers) {
customer.getAddress(); // Here EclipseLink executes a query to get the relationship.
}
感谢。
谢谢,但这种模式并没有解决我的问题。我需要EclipseLink不会使查询返回LAZY关系,我需要它返回null。 –
我建议反对“公开会议”: http://blog.jhades.org/open-session-in-view-pattern-pros-and-cons/ https:// blog。frankel.ch/the-opensessioninview-antipattern 它被认为是Hibernate中的反模式,我怀疑EclipseLink认为它好得多。 –