重新部署后,JPA实体中的ClassCastException(Glassfish 3.1.2) [英] ClassCastException in JPA Entitiy after redeploy (Glassfish 3.1.2)

查看:94
本文介绍了重新部署后,JPA实体中的ClassCastException(Glassfish 3.1.2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用Glassfish 3.1.2取消部署和部署WebArchive(.war)时,我会产生奇怪的效果.

I have a strange effect when undeploying and deploying a WebArchive (.war) with Glassfish 3.1.2.

$ asadmin undeploy myWebApp; asadmin deploy target/myWebApp.war

它正常部署,但是当我通过实体管理器获取实体bean时,它抛出异常: [#|2012-12-11T15:26:09.772+0100|SEVERE|glassfish3.1.2|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=119;_ThreadName=Thread-2;|java.lang.ClassCastException: org.YourEntity cannot be cast to org.YourEntity

It deploys normally, but when I fetch an entity bean via the entity manager, it throws an Exception: [#|2012-12-11T15:26:09.772+0100|SEVERE|glassfish3.1.2|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=119;_ThreadName=Thread-2;|java.lang.ClassCastException: org.YourEntity cannot be cast to org.YourEntity

在q.getSingleResult()调用中引发了异常.

The exception is thrown in the q.getSingleResult() call.

  @PersistenceContext(unitName = "org.my-PU")
  private EntityManager em = Persistence.createEntityManagerFactory("org.my-PU").createEntityManager();
  ...
  public YourEntity findYourEntity() throws Exception {
      TypedQuery<YourEntity> q = em.createQuery("select ye from YourEntity ye", 
              YourEntity.class);
      return q.getSingleResult();
  }

我在项目中使用了以下与JPA相关的依赖项:

I am using the following JPA related dependencies in the project:

<dependency>
  <groupId>org.eclipse.persistence</groupId>
  <artifactId>javax.persistence</artifactId>
  <version>2.0.0</version>
</dependency>

<dependency>
  <groupId>org.eclipse.persistence</groupId>
  <artifactId>eclipselink</artifactId>
  <version>2.0.2</version>
</dependency>

<dependency>
  <groupId>com.oracle</groupId>
  <artifactId>ojdbc6</artifactId>
  <version>11.1.0.7.0</version>
</dependency>

当我重新启动玻璃鱼时,该异常不再发生.取消部署/部署具有相似设置的.ear时,我从未见过该问题.有没有人看到错误并知道如何解决它?这不是一个大问题,但很烦人.

When I restart the glassfish, the exception does not occur any more. I have never seen the problem when undeploying/deploying an .ear with similar settings. Has anybody seen the error and knows how to overcome it? It is not a big problem, but annoying.

推荐答案

与类加载器绑定的资源将静态保存,直到EntityManagerFactories关闭.这些是由应用程序管理的,因此当不再需要关闭或取消部署事件时,您必须在工厂上手动调用关闭-垃圾收集也可以清除它们,但是在重新部署应用程序并再次使用它们访问它们之前不会发生不同的类加载器,导致您看到异常.

Resources tied to the classloader are held statically until the EntityManagerFactories are closed. These are application managed so you must manually call close on the factories on shutdown or undeploy events when they are no longer needed - garbage collection might clean them up as well, but it is not occurring before the app is redeployed and accesses them again with a different classloader, resulting in the exception you see.

请保留在工厂并在不再需要时关闭它,或使用注射剂,让容器为您管理生命周期.

Hold onto the factory and close it when no longer needed, or use injection and allow the container to manage it's life cycle for you.

这篇关于重新部署后,JPA实体中的ClassCastException(Glassfish 3.1.2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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