java - JPA createNamedQuery 语法

标签 java jpa orm named-query jpa-2.0

在 Pro JPA 2 (Apress) 书中,我看到过这样的例子,

EntityManager em;
Long count = em.createNamedQuery(countQueryName, Long.class).getSingleResult();

但是,api ,希望应用于 JPA 2.0,显示语法为

createNamedQuery(String name) 
Parameters:
name - the name of a query defined in metadata

只有一个参数。这本书广泛使用了该类型,那么我错过了什么?

谢谢。

最佳答案

Java EE 5 使用 JPA 1,Java EE 6 使用 JPA 2。以下是您正在寻找的方法:

createNamedQuery(java.lang.String name, java.lang.Class resultClass) :

Create an instance of TypedQuery for executing a Java Persistence query language named query. The select list of the query must contain only a single item, which must be assignable to the type specified by the resultClass argument.

Parameters: name - the name of a query defined in metadata resultClass - the type of the query result

Returns: the new query instance

Throws: IllegalArgumentException - if a query has not been defined with the given name or if the query string is found to be invalid or if the query result is found to not be assignable to the specified type

Since: Java Persistence 2.0

关于java - JPA createNamedQuery 语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3380532/

相关文章:

java - 无法反序列化 `java.util.ArrayList` 的实例

java - Hibernate JPA ManyToOne 外键映射通过 IdClass 与复合键找不到列

java - 返回 boolean 值而不是列表

java - Teamcity 代理在构建后断开连接(无法访问代理)

Java链接两个变量?

java - gradlew assemble 在 Travis-CI 上失败

java - 如何创建采用 XML 并将一些数据绑定(bind)到 JPA 注释的 POJO 的 Camel 路由?

Java 实体 - 为什么我需要一个空的构造函数?

java - 有没有办法访问 Hibernate 中的子字段?

Django 模型方法与自定义管理器