Search code examples
jpaeclipselinkclasscastexception

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.getSingleResult(); // 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?


Solution

  • 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.