简体   繁体   English

java.lang.ClassCastException:com.example.Entity无法强制转换为com.example.Entity

[英]java.lang.ClassCastException: com.example.Entity cannot be cast to com.example.Entity

I'm creating a JSF+JPA project for and GlassFish 4. A couple of times I get an exception at the at this point: 我正在为GlassFish 4创建一个JSF + JPA项目。有几次我在此时得到一个例外:

List<User> users = connection.getUserList();

for (User u : users) { //exception

... ...

    public List<User> getUserList() {

    EntityManager em = Persistence.createEntityManagerFactory("project").createEntityManager();
    List<User> users = em.createQuery("SELECT c FROM User c", User.class).getResultList();
    em.close();
    return users;
}

stacktrace: 堆栈跟踪:

javax.faces.el.EvaluationException: java.lang.ClassCastException: de.sep.ateam.sap.norris.model.User cannot be cast to de.sep.ateam.sap.norris.model.User
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:101)
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    at javax.faces.component.UICommand.broadcast(UICommand.java:315)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
    at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.ClassCastException: de.sep.ateam.sap.norris.model.User cannot be cast to de.sep.ateam.sap.norris.model.User
    at de.sep.ateam.sap.norris.controller.beans.LoginBean.doLogin(LoginBean.java:50)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at javax.el.ELUtil.invokeMethod(ELUtil.java:326)
    at javax.el.BeanELResolver.invoke(BeanELResolver.java:536)
    at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:256)
    at com.sun.el.parser.AstValue.invoke(AstValue.java:269)
    at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304)
    at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
    at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
    ... 35 more

If you are sure that you don`t have multiple versions of the same class loaded in the application server, you might try to reset the server cache. 如果您确定在应用程序服务器中没有加载同一类的多个版本,则可能会尝试重置服务器缓存。

Follow this steps: 请遵循以下步骤:

  1. Undeploy the application; 取消部署应用程序;
  2. Stop the application server; 停止应用程序服务器;
  3. Delete the osgi cache folder of your application server. 删除应用程序服务器的osgi缓存文件夹。 Ie if you are using glassfish, it is the folder: E:\\glassfish-3.1.2.2\\glassfish\\domains\\domain1\\osgi-cache 即如果你使用glassfish,它是文件夹:E:\\ glassfish-3.1.2.2 \\ glassfish \\ domains \\ domain1 \\ osgi-cache
  4. Restart the server; 重启服务器;
  5. Deploy the application. 部署应用程序。

I had the same issue, and I finally found a workaround on java.net : 我有同样的问题,我终于在java.net上找到了一个解决方法:

Copy all org.eclipse.persistence jar files from glassfish4/glassfish/modules to WEB-INF/lib . 将所有org.eclipse.persistence jar filesglassfish4/glassfish/modules复制到WEB-INF/lib Then go in your glassfish-web.xml, and set class-delegate to false . 然后进入你的glassfish-web.xml,并将class-delegate设置为false

Worked for me ! 为我工作!

我遇到了类似的问题,因为我正在序列化类,并且在更新类并且重新启动应用程序而不重新启动容器之后,序列化版本无法反序列化。

Typically this happens when there is more than one instance of the same jar in a different class loader. 通常,当在不同的类加载器中存在多个相同jar的实例时,会发生这种情况。 Most app servers have a global class loader and a web-app specific class loader (some also have others like contrib, etc.). 大多数应用服务器都有一个全局类加载器和一个特定于Web应用程序的类加载器(有些还有其他像contrib等)。 If the class loader that is higher up in the food chain has that class defined already and it gets redefined by a lower level class loader this sort of situation can occur (the same class in a different class loader is considered different even though its definition is the same). 如果食物链中较高的类加载器已经定义了该类,并且它被较低级别的类加载器重新定义,则会发生这种情况(即使它的定义是相同的,不同类加载器中的相同类被认为是不同的相同)。 The order of how/when jars get loaded is not guaranteed which could explain why you sometimes see the error and sometimes don't. 罐子如何/何时加载的顺序不能保证,这可以解释为什么你有时会看到错误,有时却没有。 Long story short, make sure you haven't already deployed the jar that contains this class somewhere else in the server that is visible to your web app. 简而言之,请确保您尚未在Web应用程序可见的服务器中的其他位置部署包含此类的jar。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.ClassCastException:com.google.gson.internal.LinkedTreeMap无法强制转换为com.example - java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to com.example java.lang.ClassCastException:java.lang.String无法强制转换为com.example.service.AlbumService - java.lang.ClassCastException: java.lang.String cannot be cast to com.example.service.AlbumService Vaadin异常:java.lang.ClassCastException:com.example.view.ForecastInputs $ 2无法强制转换为com.example.view.ForecastInputs - Vaadin exception : java.lang.ClassCastException: com.example.view.ForecastInputs$2 cannot be cast to com.example.view.ForecastInputs java.lang.ClassCastException:无法将java.util.LinkedHashMap强制转换为com.example.backendtest.Project - java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.example.backendtest.Project java.lang.ClassCastException:com.example.kada.view.pzv.PullToZoomScrollViewEx无法转换为com.example.kada.view.pzv.PullToZoomListViewEx - java.lang.ClassCastException: com.example.kada.view.pzv.PullToZoomScrollViewEx cannot be cast to com.example.kada.view.pzv.PullToZoomListViewEx java.lang.ClassCastException:[Ljava.lang.Object; 无法转换为com.xl.entity.Users - java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.xl.entity.Users java.lang.ClassCastException:[Ljava.lang.Object; 无法转换为com.entity.survey.SurveyReport - java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.entity.survey.SurveyReport java.lang.ClassCastException: com.example.slideshowapp.MainActivity 无法转换为 android.widget.AdapterView$OnItemClickListener - java.lang.ClassCastException: com.example.slideshowapp.MainActivity cannot be cast to android.widget.AdapterView$OnItemClickListener java.lang.ClassCastException: com.example.BellasHBG.LocationService cannot be cast to com.google.android.gms.location.LocationListener - java.lang.ClassCastException: com.example.BellasHBG.LocationService cannot be cast to com.google.android.gms.location.LocationListener 由以下原因引起:java.lang.ClassCastException:com.example.mayank.sunshine.SettingsActivity无法转换为android.app.Activity - Caused by: java.lang.ClassCastException: com.example.mayank.sunshine.SettingsActivity cannot be cast to android.app.Activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM