@Entity @Table(name = "testa") public class TestA implements Serializable { @Id @SequenceGenerator(name="TESTA_ID_GENERATOR", sequenceName="TESTA_ID_SEQ") @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="TESTA_ID_GENERATOR") private Long id; public TestA() { } public Long getId() { return this.id; } }
@Entity @Table(name = "testb") public class TestB implements Serializable { @Id @SequenceGenerator(name="TESTB_ID_GENERATOR", sequenceName="TESTB_ID_SEQ") @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="TESTB_ID_GENERATOR") private Long id; //uni-directional one-to-one association to TestA @OneToOne(fetch=FetchType.LAZY) @JoinColumn(name = "testa_id") private TestA testa; public TestB() { } public Long getId() { return this.id; } public TestA getTesta() { return this.testa; } public void setTesta(TestA testa) { this.testa = testa; } }
public EntityManager aquireEntityManager(String uniqueCustomerName) { EntityManagerFactory emf = this.emfMap.get(uniqueCustomerName); if(emf == null) { Map<String,String> emProperties = new HashMap<String, String>(); emProperties.put(PersistenceUnitProperties.NON_JTA_DATASOURCE, "jdbc/PostgreSQL_backend-" + uniqueCustomerName); emf = Persistence.createEntityManagerFactory("backend-jpaPU", emProperties); emf.getCache().evictAll(); this.emfMap.put(uniqueCustomerName, emf); } EntityManager em = emf.createEntityManager(); return em; }
<plugin> <artifactId>maven-antrun-plugin</artifactId> <executions> <execution> <id>JPA Enhancement</id> <phase>process-classes</phase> <configuration> <tasks> <java classname="org.eclipse.persistence.tools.weaving.jpa.StaticWeave" classpathref="maven.test.classpath" fork="true"> <arg line="-loglevel FINEST -persistenceinfo src/main/resources target/classes target/classes" /> </java> </tasks> </configuration> <goals> <goal>run</goal> </goals> </execution> </executions> </plugin>
<?xml version="1.0"?> <project name="jpa-weaving" default="weaving" basedir="./../.."> <property environment="env"/> <property name="glassfish.home" value="${env.GLASSFISH_HOME}"/> <path id="weavingclasspath"> <pathelement location="${glassfish.home}/lib/javaee.jar"/> <pathelement location="${basedir}/lib/eclipselink.jar"/> </path> <target name="define.task" description="New task definition for EclipseLink static weaving" > <taskdef name="weave" classname="org.eclipse.persistence.tools.weaving.jpa.StaticWeaveAntTask"> <classpath refid="weavingclasspath"/> </taskdef> </target> <target name="weaving" description="perform weaving" depends="define.task"> <weave source="${basedir}/target/classes" target="${basedir}/target/classes"> <classpath refid="weavingclasspath"/> </weave> </target> </project>
<plugin> <artifactId>maven-antrun-plugin</artifactId> <executions> <execution> <id>JPA Enhancement</id> <phase>process-classes</phase> <configuration> <tasks> <java classname="org.eclipse.persistence.tools.weaving.jpa.StaticWeave" classpathref="maven.test.classpath" fork="true"> <arg line="-loglevel FINEST -persistenceinfo src/main/resources target/classes target/classes" /> </java> </tasks> </configuration> <goals> <goal>run</goal> </goals> </execution> </executions> </plugin>
<?xml version="1.0"?> <project name="jpa-weaving" default="weaving" basedir="./../.."> <property environment="env"/> <property name="glassfish.home" value="${env.GLASSFISH_HOME}"/> <path id="weavingclasspath"> <pathelement location="${glassfish.home}/lib/javaee.jar"/> <pathelement location="${basedir}/lib/eclipselink.jar"/> </path> <target name="define.task" description="New task definition for EclipseLink static weaving" > <taskdef name="weave" classname="org.eclipse.persistence.tools.weaving.jpa.StaticWeaveAntTask"> <classpath refid="weavingclasspath"/> </taskdef> </target> <target name="weaving" description="perform weaving" depends="define.task"> <weave source="${basedir}/target/classes" target="${basedir}/target/classes"> <classpath refid="weavingclasspath"/> </weave> </target> </project>
<plugin> <artifactId>maven-antrun-plugin</artifactId> <executions> <execution> <id>JPA Enhancement</id> <phase>process-classes</phase> <configuration> <tasks> <java classname="org.eclipse.persistence.tools.weaving.jpa.StaticWeave" classpathref="maven.test.classpath" fork="true"> <arg line="-loglevel FINEST -persistenceinfo src/main/resources target/classes target/classes" /> </java> </tasks> </configuration> <goals> <goal>run</goal> </goals> </execution> </executions> </plugin>
<?xml version="1.0"?> <project name="jpa-weaving" default="weaving" basedir="./../.."> <property environment="env"/> <property name="glassfish.home" value="${env.GLASSFISH_HOME}"/> <path id="weavingclasspath"> <pathelement location="${glassfish.home}/lib/javaee.jar"/> <pathelement location="${basedir}/lib/eclipselink.jar"/> </path> <target name="define.task" description="New task definition for EclipseLink static weaving" > <taskdef name="weave" classname="org.eclipse.persistence.tools.weaving.jpa.StaticWeaveAntTask"> <classpath refid="weavingclasspath"/> </taskdef> </target> <target name="weaving" description="perform weaving" depends="define.task"> <weave source="${basedir}/target/classes" target="${basedir}/target/classes"> <classpath refid="weavingclasspath"/> </weave> </target> </project>
Quote: |
If you are developing your application in a Java EE environment, you only have to set fetch to javax.persistence.FetchType.LAZY, and EclipseLink persistence provider will supply all the necessary functionality. When using a one-to-one or many-to-one mapping in a Java SE environment, to configure EclipseLink JPA to perform lazy loading when the fetch attribute is set to FetchType.LAZY, configure either dynamic or static weaving. When using a one-to-one or many-to-one mapping in a Java SE environment that does not permit the use of -javaagent on the JVM command line, to configure EclipseLink JPA to perform lazy loading when annotation attribute fetch is set to javax.persistence.FetchType.LAZY, you can use static weaving. |
<build> <plugins> <plugin> <artifactId>eclipselink-staticweave-maven-plugin</artifactId> <groupId>au.com.alderaan</groupId> <version>1.0.1</version> <executions> <execution> <goals> <goal>weave</goal> </goals> <phase>process-classes</phase> </execution> </executions> </plugin> </plugins> </build>