GlassFish 4 (actually its implementation of JPA, that is, EclipseLink) cannot be lazy to load the @ManyToOne JPA relation from our Java EE 7 application. By default / impatient loading is OK, but not lazy loading.
Attitude in essence "Student":
@ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "addr_id") private Address address;
(simplified) persistence.xml is as follows:
<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>
The application uses several APIs: PrimeFaces, JSF 2.2, CDI 1.1, JPA 2.1.
Also note that EntityManager is not obtained by inserting a session into the EJB, but is created manually using Persistence.createEntityManagerFactory (...), then emf.createEntityManager (...).
Error message:
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.
I understand that for some reason, dynamic weaving of objects is not enabled. For a Java EE application, it should be as suggested by http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Advanced_JPA_Development/Performance/Weaving .
For the record, if we try to force weaving with this:
<property name="eclipselink.weaving" value="true"/>
in persistence.xml, we get another error message:
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)
Any idea how to fix this problem with lazy loading? Why is dynamic weaving not enabled by default?
Thanks.