|
Using hibernate in Glassfish it is not possible anymore to use the results of a JPQL TypedQuery, e.g.
"select p from ProductItem p"
The query executes correctly but when trying to access items a ClassCastException is thrown, e.g.
java.lang.ClassCastException: com.mycompany.domain.model.ProductItem cannot be cast to com.mycompany.domain.model.ProductItem
The problem is clearly reproducible since version 4.3.6. The latest "good" version is 4.3.5. To reproduce the issue please unzip the Attached example (self-contained maven project) and read "ReadMe.docx" located in the root folder of the zip file.
Version of hibernate can be easily changed in pom.xml (toplevel).
Debugs from
DefaultProductDao.java:
@Override
public List<String> getAllProductItemNames() {
final TypedQuery<ProductItem> q = getEntityManager().createQuery("select p from ProductItem p", ProductItem.class);
final List resultList = q.getResultList();
List<String> names = new ArrayList<>();
if (!resultList.isEmpty()) {
Class c1 = ProductItem.class;
Class c2 = resultList.get(0).getClass();
ClassLoader cl1 = c1.getClassLoader();
ClassLoader cl2 = c2.getClassLoader();
System.out.println("Class 1: " + c1.getCanonicalName());
System.out.println("Class 2: " + c2.getCanonicalName());
System.out.println("Classloader 1: " + getClassLoaderString(cl1));
System.out.println("Classloader 2: " + getClassLoaderString(cl2));
}
for (Object o : resultList) {
ProductItem p = (ProductItem) o;
names.add(p.getName());
}
return names;
}
-->
Class 1: com.mycompany.domain.model.ProductItem]]
Class 2: com.mycompany.domain.model.ProductItem]]
Classloader 1: Hash: 808590047 - Loader: WebappClassLoader (delegate=true; repositories=WEB-INF/classes/)]]
Classloader 2: Hash: 697243695 - Loader: WebappClassLoader (delegate=true)]]
Related issue: http://stackoverflow.com/questions/26657682/hibernate-4-3-6-and-glassfish-4-0-jpa-2-1-object-is-not-an-instance-of-declaring
|