开发者

Spring 3.0 ORM with JPA 2.0 ClassCastException

开发者 https://www.devze.com 2022-12-17 18:40 出处:网络
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:

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 开发者_Python百科= 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();
    }

}

How do I solve this problem?


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));
    ...
}

It would be better to create an issue.

0

精彩评论

暂无评论...
验证码 换一张
取 消