ClassCastException: JPA -> Bean value (different ClassLoaders)
I get a ClassCastException when assigning an object from JPA to an attribute of a ManagedBean:
Object r = query.getSingle开发者_Go百科Result(); // javax.persistence.Query
ClassLoader c1 = this.getClass().getClassLoader();
ClassLoader c2 = r.getClass().getClassLoader();
user = (User) r; // blubb.model.User
The problem is that c1 (ManagedBean) and c2 (EclipseLink) are different ClassLoaders:
c1: WebappClassLoader (delegate=true; repositories=WEB-INF/classes/)
c2: WebappClassLoader (delegate=true)How can I fix this?
What is your environment? Are you using Java EE, Spring, OSGi? Which server, WLS, WAS, GF?
Did you redeploy your application? Is the persistence unit managed or non-managed?
It could be that you redeployed your application, but never closed the EntityManagerFactory, so it is still deployed with the old classes.
I've had same problem. Simple JSF project with EclipseLink 2.5.2 JPA inside of Glassfish 4.1.1.
Solved by correctly closing the EntityManagerFactory
. I reccomend to use ServletContextListener
(@WebListener
) (take look here or here).
精彩评论