AbstractMethodError when creating a typed query with Hibernate 3.6.3 and JPA 2.0

I am using Hibernate and JPA for a small project.

Somehow, while trying to get a typed query,

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

      

rushes; org.hibernate.ejb.EntityManagerImpl - From hibernate-entitymanager-3.3.2.GA.jar.

This is not good enough to throw 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 ok, 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;
}

      

+3


source to share


1 answer


You have enough mix of Hibernate and JPA versions. On the subject line, you are specifying Hibernate version 3.6.3 and JPA version 2.0. According to the body text, EntityManagerImpl is version 3.3.2.GA. This versioning grid is causing your problem.

TypedQuery was introduced in JPA 2.0, and Hibernate has implemented this specification since 3.5.X. You now have an EntityManager interface with



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

      

but the actual implementation does not implement such a method. This is why you are getting AbstractMethodError. Your second query works great because it uses JPA 1.0 constructs with one of its implementations (3.3.2.GA.) Just use the implementation from Hibernate version 3.6.3 (or better never).

+5


source







All Articles