首先,我想为我对 Hibernate 的不熟悉表示歉意。我最近才进入这个领域,距离专家还很远。
我有三个表:契约(Contract)、产品以及它们之间用于定义多对多关系的链接表。
我正在尝试编写一个 HQL 查询来返回包含一系列产品的所有契约(Contract)。不幸的是,IN
语法的工作方式类似于 Any
,而不是 All
。因此,如果我想要包含 ProductA、ProductB 和 ProductC 的所有合约,IN
关键字将返回包含其中任何一个产品的合约,而不是包含所有这些产品的合约。
我应该如何构建我的 HQL 查询?
最佳答案
为什么您期望 IN
表现得像 AND
?据我所知,IN
是一种OR
,而不是AND
。因此 IN
可能不是您要查找的内容。看看Hibernate的Expressions特别是:
- HQL functions that take collection-valued path expressions:
size()
,minelement()
,maxelement()
,minindex()
,maxindex()
, along with the specialelements()
andindices
functions that can be quantified using some, all, exists, any, in.[...]
The SQL functions any, some, all, exists, in are supported when passed the element or index set of a collection (elements and indices functions) or the result of a subquery (see below):
[...]
from Show show where 'fizard' in indices(show.acts)
关于java - Hibernate "IN"子句为 ALL 而不是 ANY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1603582/