AbstractMethodError when creating typed query with Hibernate 3.6.3 and JPA 2.0

自闭症网瘾萝莉.ら 提交于 2019-12-07 13:13:56

问题


I'm using Hibernate and JPA for a small project.

Somehow when trying to obtain an typed Query, the

java.lang.AbstractMethodError: org.hibernate.ejb.EntityManagerImpl.createQuery(Ljava/lang/String;Ljava/lang/Class;)Ljavax/persistence/TypedQuery

is thrown; org.hibernate.ejb.EntityManagerImpl is from hibernate-entitymanager-3.3.2.GA.jar .

This is not okay throwing the above exception:

  public Account read(Account entity) {
        EntityManager em = ManagedEntityManagerFactory.getEntityManager();

        String jpql = JPQLGenerator.readAccount();
        TypedQuery<Account> typedQuery =
                em.createQuery(jpql, Account.class);
        typedQuery.setParameter("accountId", entity.getAccountId());
        return typedQuery.getSingleResult();
    }

This is okay, however:

public Account read(Account entity) {
    EntityManager em = ManagedEntityManagerFactory.getEntityManager();

    String jpql = JPQLGenerator.readAccount();

    Query query =
            em.createQuery(jpql);
    query.setParameter("accountId", entity.getAccountId());
    Account account = null;
    Object obj = query.getSingleResult();
    if(obj instanceof Account) {
        account = (Account)obj;
    }
    return account;
}

回答1:


You have quite a mix of Hibernate and JPA versions. In subject line you mention Hibernate version 3.6.3 and JPA version 2.0. According body text EntityManagerImpl is version 3.3.2.GA. This mesh up with versions causes your problem.

TypedQuery was introduced in JPA 2.0 and Hibernate implements this specification since 3.5.X. Now you have EntityManager interface with

<T> TypedQuery<T> createQuery(CriteriaQuery<T> criteriaQuery)

but actual implementation does not implements such a method. That's why you get AbstractMethodError. Your second query works fine, because it uses JPA 1.0 constructs with one of the it's implementations (3.3.2.GA.) Just use implementation from Hibernate version 3.6.3 (or better even never).



来源:https://stackoverflow.com/questions/9860181/abstractmethoderror-when-creating-typed-query-with-hibernate-3-6-3-and-jpa-2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!