简体   繁体   中英

Eclipselink: Difference between static and dynamic weaving

I use eclipselink 2.6.3 and have the following code:

public Temp getTemp() {
    EntityManager em=emf.createEntityManager();
    String queryString="SELECT a FROM Temp a";
    EntityGraph<Temp> eg = em.createEntityGraph(Temp.class);
    eg.addAttributeNodes("id");
    //eg.addAttributeNodes("name");
    Query query = em.createQuery(queryString);
    query.setHint("javax.persistence.fetchgraph", eg);
    List<Temp> items=query.getResultList();
    em.close();// ENTITYMANAGER IS CLOSED 
    return items.get(0);
}

public void temp(){
    Temp temp=getTemp();
    System.out.println("id:"+temp.getId());
    System.out.println("name:"+temp.getName());
}

Situation 1: When weaving is static ( <property name="eclipselink.weaving" value="static"/> + de.empulse.eclipselink weaving plugin) and we do temp.getName() onemore SQL query is executed and necessary data is loaded. In spite of the fact we did close entity manager. I expected to get exception at temp.getName().

Situation 2: However, when weaving is dynamic ( <property name="eclipselink.weaving" value="true"/> ) I get exception:

java.lang.ClassNotFoundException: org.eclipse.persistence.internal.jpa.EntityManagerImpl not found by com.temp [57]
at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1574)
at org.apache.felix.framework.BundleWiringImpl.access$400(BundleWiringImpl.java:79)
at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:2018)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at com.temp.Temp._persistence_checkFetched(Temp2.java)
at com.temp.Temp._persistence_get_name(Temp2.java)
at com.temp.Temp.getName(Temp.java:44)

How to explain this difference? Is this a bug?

If you running your application as desktop(standalone) , tomcat or jetty, then dynamic weaving is not available by default, and lazy fetch is not supported , all lazy fetch will be treated as eager.

Regarding the static weaving, the below is from eclipselink tutorial: http://wiki.eclipse.org/EclipseLink/Examples/JPA/Tomcat_Web_Tutorial

Limitations to JPA
As Tomcat is not a Java EE 5 compatible server, there are some limitiations to JPA. No dynamic weaving (instrumentation)
- static weaving of entities is still available via EclipseLink No @EJB injection of a session bean (containing the EntityManager) is available
- use the persistence factory and manager directly No @PersistenceContext injection of a container managed persistence unit is available
- use Persistence.createEntityManagerFactory(JTA_PU_NAME)

to make your entities support static weaving : http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Advanced_JPA_Development/Performance/Weaving/Static_Weaving

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM