Why doesn’t Entitymanager have the createQuery method with typed return?

Asked

Viewed 111 times

2

I am following the project of the book Spring-MVC of Casa Do Código, using the latest version of Hibernate, a 4.0.3.

But in a moment, when the book indicated the creation of a typed query through the following code:

// manager é um objeto da classe javax.persistence.EntityManager
manager.createQuery("minha query", MinhaEntidade.class);

I noticed that my object EntityManager responded to the signature only of a method called createQuery, where the only accepted parameter is a String, and no longer a class as a second parameter.

However, someone can tell me if this method is no longer used, if it is my mistake or what may be different from the book’s design?

1 answer

3


This method exists in Java EE 7 (which has JPA 2.1):

http://docs.oracle.com/javaee/7/api/javax/persistence/EntityManager.html#createQuery-java.lang.String-java.lang.Class-

In Java EE 6 (which has JPA 2.0):

http://docs.oracle.com/javaee/6/api/javax/persistence/EntityManager.html#createQuery(java.lang.String,%20java.lang.Class)

But not in Java EE 5 (which has JPA 1.0):

http://docs.oracle.com/javaee/5/api/javax/persistence/EntityManager.html

Seeing the javadoc of the interface TypedQuery, which is the type of return method you want:

Since:
Java Persistence 2.0

That is, if this method did not appear to you it is because you probably have a JAR to use JPA 1.0, not JPA 2.0 or 2.1.

You can download the latest JAR to use JPA 2.0 (Java EE 6) on Hibernate here:

http://mvnrepository.com/artifact/org.hibernate.javax.persistence/hibernate-jpa-2.0-api/1.0.1.Final

Or to JPA 2.1 (Java EE 7):

http://mvnrepository.com/artifact/org.hibernate.javax.persistence/hibernate-jpa-2.1-api/1.0.0.Final

  • 1

    On the fly, thank you very much

Browser other questions tagged

You are not signed in. Login or sign up in order to post.