等同于JPA 1.0的TypedQuery [英] TypedQuery equivalent for JPA 1.0
问题描述
我对JPA还是很陌生,我正在使用Apress JPA2教科书来学习它.我试图做这本书的第一个例子.以下代码行给我一个错误:
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(从员工e的SELECT e",Employee.class);
TypedQuery query = em.createQuery("SELECT e FROM Employee e", Employee.class);
表示无法将TypedQuery解析为一种类型.经过一段时间的努力,我意识到我正在使用JPA版本1,该版本不包含TypedQuery而是仅包含Query接口.
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 contain TypedQuery but just Query interface.
我的问题是JPA版本1中是否存在等效的声明.请帮助.预先感谢.
My question is whether there is an equivalent statement in JPA version 1. Kindly help. Thanks in advance.
推荐答案
由于TypedQuery是从JPA-2.0引入的,因此必须使用Query接口.
As TypedQuery was introduced from JPA-2.0, have to go for Query interface.
1)本机查询,以映射查询的结果类型(失去可移植性).
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)创建查询&然后将其显式转换为结果类型(更可取).
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
这篇关于等同于JPA 1.0的TypedQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!