开发者

TypedQuery equivalent for JPA 1.0

I am quite new to JPA and I am using Apress JPA2 text book to learn it. I was trying to do the first example from the book. This following line of code gives me an error:

TypedQuery query = em.createQuery("SELECT e FROM Employee e", Employee.class);

saying that TypedQuery cannot be resolved to a type. After struggling for sometime I realised that I am using JPA version 1 which does not cont开发者_JAVA技巧ain TypedQuery but just Query interface.

My question is whether there is an equivalent statement in JPA version 1. Kindly help. Thanks in advance.


As TypedQuery was introduced from JPA-2.0, have to go for Query interface.

1) Native query to map the result type for the query (loosing portability).

Query selectQuery = entityManager.createNativeQuery("SELECT
 e FROM Employee e", Employee.class);

2) Creating query & then explicitly casting it to the result type (more preferable).

Query selectQuery = entityManager.createQuery("SELECT e FROM Employee e")
List<Employee> employees = (List<Employee>)selectQuery.getResultList(); //Multiple Result
Employee employee = (Employee)selectQuery.getSingleResult(); //Single Result
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜