有什么方法可以在 Hibernate 中使用单个选择查询而不是多个选择查询吗?
我正在使用 Spring + Hibernate(HQL)。
我有一个要求,我需要多次触发选择查询,因为每次参数都不同。
例如
SELECT * FROM MY_TABLE WHERE name=?和年龄=?
输入将是
"John", 30
"Nick", 29
"Joe", 32
等等..它们可以有任意数量。
这导致 n 个查询,其中 n 是输入的数量
例如:
SELECT * FROM MY_TABLE WHERE name=John and age=30
SELECT * FROM MY_TABLE WHERE name=Nick and age=29
SELECT * FROM MY_TABLE WHERE name=Joe and age=32
在 hibernate 中是否有一种方法,我只能对此进行一个查询,而不是多个选择查询
例如:SELECT * FROM MY_TABLE WHERE (name=John andage=30) or (name=Nick andage=29) or (name=Joe andage=32)
或者任何其他优化方式?
I am using Spring + Hibernate(HQL).
I have a requirement where I need to fire select query multiple times as parameters are different each time.
For Example
SELECT * FROM MY_TABLE WHERE name=? and age=?
inputs will be
"John", 30
"Nick", 29
"Joe", 32
etc.. there could be any number of them.
This is leading to n number of queries where n is the number of inputs
Ex:
SELECT * FROM MY_TABLE WHERE name=John and age=30
SELECT * FROM MY_TABLE WHERE name=Nick and age=29
SELECT * FROM MY_TABLE WHERE name=Joe and age=32
is there a way in hibernate where I can have only one query for this instead of multiple select queries
Ex:SELECT * FROM MY_TABLE WHERE (name=John and age=30) or (name=Nick and age=29) or (name=Joe and age=32)
Or any other optimized way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你可以使用UNION,但据我所知,HQL不支持UNION。如果此限制仍然存在,那么您可能必须回退到本机 SQL 才能使用此功能
you could use UNION, though as far as I know, UNION was not supported in HQL. If this restriction still holds, then you may have to fall back to native SQL to use this
如果您使用
Criteria
查询,则可以使用Disjunction
:If you use a
Criteria
query, you could use aDisjunction
: