在 JPA 中加入命名查询
我想知道是否可以在 JPA 中加入 2 个命名查询。作为示例,我有两个以下命名查询
1 - 获取所有活动用户
2 - 获取给定公司的所有用户
我是否可以加入上面两个命名查询并获取 获取给定公司的所有活跃用户。
I want to know whether I can join 2 named queries in JPA. As an example I have two following named queries
1 - Get all the active users
2 - get all the users for a given company
Is it possible for me to join above two named queries and get
get all the active users for a given company.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要为此使用
NamedQuery
。直接将查询字符串传递给该方法是最好的选择。您必须了解
createNamedQuery(String name)
方法采用命名查询的名称。而createNamedQuery(String qlString)
接受查询字符串,因此这符合您的需求。或者
为此目的创建一个单独的
NamedQuery
。Don't use
NamedQuery
for this. Directly passing a query string to the method is your best bet.You must understand that the method
createNamedQuery(String name)
takes name of a named query. Whereas,createNamedQuery(String qlString)
takes a query string, so this fits your need.Or
Create a separate
NamedQuery
for this very purpose.不,那不可能。要么编写一个智能命名查询,它可以使用参数来表达所有情况(如果可能),要么使用多个命名查询。
如果您使用 JPA 2.0,Criteria API 可能是编写动态查询的另一个(更好)选项。
No, that's not possible. Either write a smart named query that can take parameters to express all cases (if possible) or use several named queries.
And if you are using JPA 2.0, the Criteria API might be another (better) option to write dynamic queries.