【发布时间】:2013-09-17 23:29:54
【问题描述】:
GlassFish 4(实际上是它的 JPA 实现,即 EclipseLink)无法从我们的 Java EE 7 应用程序延迟加载 @ManyToOne JPA 关系。默认/急切加载是可以的,但不能延迟加载。
“学生”实体中的关系是:
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "addr_id")
private Address address;
(简化的)persistence.xml 看起来像:
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="foo-PU" transaction-type="JTA">
<jta-data-source>jdbc/foo-DS</jta-data-source>
<class>foo.domain.Student</class>
<class>foo.domain.Address</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="eclipselink.target-database" value="PostgreSQL"/>
<property name="eclipselink.logging.level" value="FINE"/>
</properties>
</persistence-unit>
</persistence>
该应用程序使用多个 API:PrimeFaces、JSF 2.2、CDI 1.1、JPA 2.1。
还要注意,EntityManager 不是通过注入会话 EJB 获得的,而是使用 Persistence.createEntityManagerFactory(...) 然后 emf.createEntityManager(...) 手动创建的。
错误信息是:
WARNING: Reverting the lazy setting on the OneToOne or ManyToOne attribute [address] for the entity class [class foo.domain.Student] since weaving was not enabled or did not occur.
我的理解是,由于某种原因,没有启用实体的动态编织。正如 http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Advanced_JPA_Development/Performance/Weaving 所建议的那样,对于 Java EE 应用程序应该是这样的。
为了记录,如果我们尝试使用这个来强制编织:
<property name="eclipselink.weaving" value="true"/>
在persistence.xml中,然后我们得到另一个错误消息:
SEVERE: Error Rendering View[/student/studentList.xhtml]
javax.el.ELException: /student/studentList.xhtml @24,81 value="#{studentController.selectedCode}": Exception [EclipseLink-30005] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.PersistenceUnitLoadingException
Exception Description: An exception was thrown while searching for persistence archives with ClassLoader: WebappClassLoader (delegate=true; repositories=WEB-INF/classes/)
Internal Exception: javax.persistence.PersistenceException: Exception [EclipseLink-28022] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Value [true] for the property [eclipselink.weaving] is incorrect when global instrumentation is null, value should either be null, false, or static.
at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:114)
at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
at javax.faces.component.UIOutput.getValue(UIOutput.java:174)
at javax.faces.component.UIInput.getValue(UIInput.java:291)
at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getValue(HtmlBasicInputRenderer.java:205)
(...)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
知道如何解决这个延迟加载问题吗?为什么默认不开启动态编织?
谢谢。
【问题讨论】:
-
您为什么使用 JTA 数据源等,但从 Persistence 类获取 EMF,就好像它是 java SE 应用程序一样?从它的声音来看,容器没有部署持久性单元 - 这是容器挂钩代理以编织类所必需的。您必须查看持久性单元是如何在应用程序中打包的,以确定为什么没有发生这种情况,可能是打包不正确或缺少资源引用。
-
JPA 不支持多租户,因此我们手动创建 EMF 和 EM 以便能够将它们动态绑定到正确的数据源。这适用于许多应用服务器。我们还使用 JTA 数据源来支持分布式事务。这些选择不应该对我们遇到的问题产生影响。现在其他持久性操作工作正常(读取、查找、更新......),所以持久性单元配置应该没问题。 GlassFish 日志也没有提到其他问题,我可以看到 PU 正在初始化。
-
我们遇到了同样的问题。你解决过这个问题吗?
-
这里也一样。有任何修复或解决方法吗?
标签: jpa glassfish eclipselink