Servlet 异常 +类转换异常 +玻璃鱼 + Netbeans + JPA 实体 +瓦丁
我收到此错误:
StandardWrapperValve[Vaadin Servlet]:PWC1406:servlet Vaadin Servlet 的 Servlet.service() 抛出异常 com.delhi.entities.Category 无法转换为 com.delhi.entities.Category
java.lang.ClassCastException:当我尝试在 glassfish v2 上运行 web 应用程序时,
。类别是一个 JPA 实体对象,
根据服务器日志,有问题的代码是:
for (Category c : categories) {
mymethod();
}
类别源自:
List<Category> categories = q.getResultList();
知道出了什么问题吗?
I get this error:
StandardWrapperValve[Vaadin Servlet]: PWC1406: Servlet.service() for servlet Vaadin Servlet threw exception
java.lang.ClassCastException: com.delhi.entities.Category cannot be cast to com.delhi.entities.Category
when I try to run my webapps on glassfish v2.
Category is a JPA entity object
the offending code according to the server log is:
for (Category c : categories) {
mymethod();
}
categories is derived from:
List<Category> categories = q.getResultList();
Any idea what went wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是一个类加载器问题。如果一个类由不同的类加载器加载,则它的对象不能互相分配。您可能已将一个对象从一个 WAR 传递到另一个 WAR 中。有几个选项可以解决这个问题:
This is a class loader issue. If a class is loaded by different class loaders, it's objects cannot be assigned to each other. You have probably passed an object from one WAR into another one. There are several options to resolve this:
我曾经遇到过同样的问题,我的环境如下:
问题是,在战争的项目设置中,我检查了[x]运行>保存时部署。这导致每次我点击保存时都会部署战争项目。它有时会导致 PermGen(内存)问题并且无法正确部署 EAR(因为例如在取消部署和部署 EAR 之间 - 这个“疯狂”的 Netbeans 正在部署这场战争)。
解决方案:如果 Netbeans &&使用 EAR,然后在项目属性中取消选中保存时部署。
编辑:
看来这个错误与
I once had the same problem and the environment I had was following:
The problem was that in war's project settings I had checked [x] Run>Deploy on save. This was causing deploying war project everyime I hit save. It was sometimes leading to PermGen (memory) problems and unability to deploy EAR correctly (because e.g. in between undeploying and deploying EAR - this "crazy" Netbeans was deploying this war).
Solution: If Netbeans && using EAR, then uncheck deploy on save in project properties.
EDIT:
it seems that this error is connected with
我今天也遇到了同样的问题。解决方案是在使用后关闭EntityManagerFactory。
这个答案帮助了我:
https://stackoverflow.com/a/13823219/2455506
I've had same problem today. Solution was closing EntityManagerFactory after use.
This answer helped me:
https://stackoverflow.com/a/13823219/2455506
我在使用 Glassfish v2 和 Glassfish v3 时也遇到了这个问题。
我可以问您一个问题:您是否在部署应用程序时尝试初始化任何持久性对象(通过启动时加载的 servlet 或上下文侦听器)?
就像 bguiz,我注意到这个问题只发生在重新部署时。新部署到刚重新启动的 Glassfish 服务器上,永远不会出现此问题。
就像 FelixM 提到的,我'我确信这是一个类加载器问题,但我不认为这是多次战争的问题(我只在我的服务器上部署了 1 个)。在 Glassfish 3 中,我可以看到我的 WAR 使用 2 个 Glassfish“引擎”。一种用于网络(war),一种用于 jpa。据我了解,这些是不同的容器,每个容器都有自己的类加载器。我猜 Glassfish v2 也以同样的方式工作。
我正在使用 Spring 并在(重新)部署时(重新)初始化一些持久性对象。我的想法是,当 Web 引擎重新初始化战争时,jpa 引擎仍在使用旧的类定义。通常,如果我在初次失败后重试重新部署,它可能会成功(有时可能需要多次重试,但最终我可以在不重新启动的情况下使其成功 - Glassfish v3 比 v2 取得更好的成功)。
此时,我认为这两个类加载器不同步,或者重新部署时存在某种竞争条件,从而允许此操作有时成功。我尝试强制类加载器,编写这样的代码
,但似乎没有任何影响。
我还想知道消除启动时的初始化是否可以解决问题,让应用服务器有时间在使用任何 jpa 类之前重新同步两个引擎(这就是我问后续问题的原因)。
I'm experiencing this problem too with Glassfish v2 and Glassfish v3.
Can I ask you a question: Are you attempting to initialize any persistence object when the application is deployed (through a servlet loaded on startup or a context listener)?
Like bguiz, I've noticed this problem only happens on redeploy. A new deploy to a freshly restarted Glassfish server, never has this problem.
Like FelixM mentioned, I'm convinced this is a class loader issue, however I don't believe it's an issue with multiple wars (I only have 1 deployed to my server). In Glassfish 3, I can see that my WAR is utilizing 2 Glassfish "engines". One for the web(war) and one for the jpa. From what I understand, these are different containers each with their own classloader. I'm guessing Glassfish v2 works in the same manner.
I'm using Spring and (re)initialize some persistence objects on (re)deploy. What I'm thinking, is that while the web engine is reinitializing the war, the jpa engine is still using the old class definitions. Often if I retry the redeploy after this initial failure, it may succeed (sometimes it may take more than one retry but eventually I can get it to succeed without a restart - having better success with Glassfish v3 than v2).
At this point I'm thinking that either these two classloaders are out of sync or there is some sort of race condition on redeploy allowing this operation to sometimes succeed. I've tried to force the classloader, writing code like this
but it didn't seem to have any affect.
I'm also wondering if eliminating the initialization at startup could fix the problem, giving the appserver time to resynchronize both engines before using any jpa classes (which is why I asked my follow up question).
我的观察是,只有在使用热重新部署或静态重新部署时才会发生这种情况。当然,这仅适用于当您收到类转换异常且其中 to 和 from 类相同时。
解决方法:
IMO我认为类加载器无法重新加载类并且旧版本是重复使用,导致错误。
本文没有直接讨论此错误,但它是很好的背景信息类加载器如何工作。
My observation is that it only happens when using a hot redeploy or a static redeploy. This only applies, of course, if you get a class cast exception where both the to and from classes are the same.
Workarounds:
IMO I think the class loader was unable to reload the class and the old version was reused, resulting in the error.
This article doesn't talk about this error directly, but it is good background info on how the class loader works.