Eclipselink:静态和动态编织之间的区别 [英] Eclipselink: Difference between static and dynamic weaving

查看:161
本文介绍了Eclipselink:静态和动态编织之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用eclipselink 2.6.3,并具有以下代码:

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());
}

情况1: 当编织是静态的(<property name="eclipselink.weaving" value="static"/> + de.empulse.eclipselink编织插件)并且我们执行temp.getName()时,将执行SQL查询并加载必要的数据.尽管我们确实关闭了实体经理.我希望在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().

情况2: 但是,当编织是动态的(<property name="eclipselink.weaving" value="true"/>)时,出现异常:

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?

推荐答案

如果您以桌面(独立),tomcat或码头运行应用程序,则默认情况下不支持动态编织,并且不支持惰性获取,所有惰性提取将被视为渴望.

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.

关于静态编织,以下内容来自eclipselink教程: http://wiki.eclipse.org/EclipseLink/Examples/JPA/Tomcat_Web_Tutorial

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

对JPA的限制
由于Tomcat不是Java EE 5兼容服务器, JPA有一些局限性.没有动态编织 (仪器)
-实体的静态编织仍然可以通过 EclipseLink否@EJB注入会话bean(包含 EntityManager)可用
-使用持久性工厂和经理 直接没有@PersistenceContext注入容器管理 持久性单元可用
- 采用 Persistence.createEntityManagerFactory(JTA_PU_NAME)

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)

使您的实体支持静态编织: http://wiki.eclipse.org/EclipseLink/UserGuide/JPA /Advanced_JPA_Development/Performance/Weaving/Static_Weaving

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

这篇关于Eclipselink:静态和动态编织之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆