java.lang.ClassCastException: $Proxy111 cannot be cast to javax.persistence.TypedQuery

Spring JIRA | Ke CAI | 7 years ago
  1. 0

    see: http://stackoverflow.com/questions/2101500/spring-3-0-orm-with-jpa-2-0-classcastexception/2102328#2102328 I'm trying to use JPA 2.0 in Spring 3.0 ORM. The JPA vendor is Hibernate 3.5.0-Beta-3. It works well with JPQL, but when I tried to use CriteriaQuery, an exception happens: java.lang.ClassCastException: $Proxy50 cannot be cast to javax.persistence.TypedQuery at $Proxy38.createQuery(Unknown Source) at com.absorbx.retailx.dao.impl.ShopDaoImpl.findByCrieria(ShopDaoImpl.java:30) at com.absorbx.retailx.dao.SimpleDaoTest.testFindByCriteria(SimpleDaoTest.java:39) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) The DAO code: @Repository public class ShopDaoImpl implements ShopDao { @PersistenceContext transient EntityManager entityManager; @Override public Shop findByCrieria() { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<Shop> c = cb.createQuery(Shop.class); Root<Shop> shop = c.from(Shop.class); c.select(shop).where(cb.equal(shop.get("name"), "petstore")); TypedQuery<Shop> q = entityManager.createQuery(c); return q.getSingleResult(); } } It seems to be a bug in Spring: org/springframework/orm/jpa/SharedEntityManagerCreator.java: if (result instanceof Query) { Query query = (Query) result; ... result = Proxy.newProxyInstance(Query.class.getClassLoader(), new Class[] {Query.class}, new DeferredQueryInvocationHandler(query, target)); ... } Good catch. Spring is checking to see if the query is an instance of Query, and generates the proxy of that type. Unfortunately, TypedQuery is a subtype of Query, and the generated proxy will still only implement Query. TypedQuery was introduced in JavaEE 6, so it's understandable why Spring doesn't handle it, although Spring 3 is supposed to handle JavaEE 6 properly. Definitely a bug.

    Spring JIRA | 7 years ago | Ke CAI
    java.lang.ClassCastException: $Proxy111 cannot be cast to javax.persistence.TypedQuery
  2. 0

    see: http://stackoverflow.com/questions/2101500/spring-3-0-orm-with-jpa-2-0-classcastexception/2102328#2102328 I'm trying to use JPA 2.0 in Spring 3.0 ORM. The JPA vendor is Hibernate 3.5.0-Beta-3. It works well with JPQL, but when I tried to use CriteriaQuery, an exception happens: java.lang.ClassCastException: $Proxy50 cannot be cast to javax.persistence.TypedQuery at $Proxy38.createQuery(Unknown Source) at com.absorbx.retailx.dao.impl.ShopDaoImpl.findByCrieria(ShopDaoImpl.java:30) at com.absorbx.retailx.dao.SimpleDaoTest.testFindByCriteria(SimpleDaoTest.java:39) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) The DAO code: @Repository public class ShopDaoImpl implements ShopDao { @PersistenceContext transient EntityManager entityManager; @Override public Shop findByCrieria() { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<Shop> c = cb.createQuery(Shop.class); Root<Shop> shop = c.from(Shop.class); c.select(shop).where(cb.equal(shop.get("name"), "petstore")); TypedQuery<Shop> q = entityManager.createQuery(c); return q.getSingleResult(); } } It seems to be a bug in Spring: org/springframework/orm/jpa/SharedEntityManagerCreator.java: if (result instanceof Query) { Query query = (Query) result; ... result = Proxy.newProxyInstance(Query.class.getClassLoader(), new Class[] {Query.class}, new DeferredQueryInvocationHandler(query, target)); ... } Good catch. Spring is checking to see if the query is an instance of Query, and generates the proxy of that type. Unfortunately, TypedQuery is a subtype of Query, and the generated proxy will still only implement Query. TypedQuery was introduced in JavaEE 6, so it's understandable why Spring doesn't handle it, although Spring 3 is supposed to handle JavaEE 6 properly. Definitely a bug.

    Spring JIRA | 7 years ago | Ke CAI
    java.lang.ClassCastException: $Proxy111 cannot be cast to javax.persistence.TypedQuery
  3. 0

    Memcached Java客户端2.6.1发布 - 编程语言 - ITeye资讯

    iteye.com | 1 year ago
    java.lang.ClassCastException: cannot be cast to
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0

    cannot be cast to javax.servlet.Filter

    Oracle Community | 6 years ago | YE
    java.lang.ClassCastException: cannot be cast to javax.servlet.Filter
  6. 0

    ClassCastException at org.eclipse.zest.core.viewers.internal.GraphModelEntityFactory.doBuildGraph(GraphModelEntityFactory.java:87)

    Eclipse Bugzilla | 7 years ago | phil_fj12
    java.lang.ClassCastException: [...] cannot be cast to org.eclipse.zest.core.viewers.IGraphEntityContentProvider

    Not finding the right solution?
    Take a tour to get the most out of Samebug.

    Tired of useless tips?

    Automated exception search integrated into your IDE

    Root Cause Analysis

    1. java.lang.ClassCastException

      $Proxy111 cannot be cast to javax.persistence.TypedQuery

      at $Proxy107.createQuery()
    2. Unknown
      $Proxy107.createQuery
      1. $Proxy107.createQuery(Unknown Source)
      1 frame