JPA createNamedQuery 语法

发布于 2024-09-11 23:39:15 字数 490 浏览 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

与单个参数一样的语法。这本书广泛使用了这种类型,那么我错过了什么?

谢谢。

In Pro JPA 2 (Apress) book, I have seen examples like,

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

But, the api , hopefully applied to JPA 2.0, shows the syntax as

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

with a single parameter. The book uses the type extensively, so what am I missing?

thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水溶 2024-09-18 23:39:15

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

createNamedQuery(java.lang.String 名称, java.lang.Class resultClass):

创建 TypedQuery 实例以执行 Java 持久性
查询语言命名为查询。查询的选择列表必须包含
只有一个项目,它必须可分配给指定的类型
resultClass 参数。

参数
name - 元数据中定义的查询的名称
resultClass - 查询结果的类型

返回:新的查询实例

抛出IllegalArgumentException - 如果尚未使用给定名称定义查询,或者发现查询字符串无效,或者发现查询结果为不可分配给指定类型

:Java Persistence 2.0

Java EE 5 uses JPA 1 and Java EE 6 uses JPA 2. Here's the method you're looking for:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文