Java HQL:多个联接集合查询不返回任何内容
我正在尝试编写一个HQL查询,返回以下内容: 他们的属性标签在我的包含标签中Java HQL:多个联接集合查询不返回任何内容,java,grails,hql,Java,Grails,Hql,我正在尝试编写一个HQL查询,返回以下内容: 他们的属性标签在我的包含标签中 或者我的财产标签在其包含标签中, 并且他们的属性标签不在我的排除标签中, 我的财产标签不在它们的标签中 以下是到目前为止我得到的信息: def thePropertyTags = this.propertyTags if(thePropertyTags == null || thePropertyTags.size() == 0) { thePropertyTags = [ Tag.UNUSED_TAG ] }
或者我的财产标签在其包含标签中,
并且他们的属性标签不在我的排除标签中,
我的财产标签不在它们的标签中 以下是到目前为止我得到的信息:
def thePropertyTags = this.propertyTags
if(thePropertyTags == null || thePropertyTags.size() == 0) {
thePropertyTags = [ Tag.UNUSED_TAG ]
}
def theInclusions = this.inclusionTags
if(theInclusions == null || theInclusions.size() == 0) {
theInclusions = [ Tag.UNUSED_TAG ]
}
def theExclusions = this.exclusionTags
if(theExclusions == null || theExclusions.size() == 0) {
theExclusions = [ Tag.UNUSED_TAG ]
}
List<MyDomain> objects = MyDomain.executeQuery("""
SELECT DISTINCT o
FROM MyDomain o
JOIN o.propertyTags as propertyTags
JOIN o.exclusionTags as exclusions
JOIN o.inclusionTags as inclusions
WHERE o.id != :id
AND o.isActive = true
AND (
exclusions IS NULL
OR exclusions IS EMPTY
OR exclusions NOT in (:propertyTags)
)
AND propertyTags NOT in (:exclusions)
AND (
inclusions in (:propertyTags)
OR propertyTags in (:inclusions)
)
""", [id: id, inclusions: theInclusions, exclusions: theExclusions, propertyTags: thePropertyTags])
我看到的第一个问题是,您正在对排除标记进行内部联接。这将确保它只返回具有排除标记的记录。此外,您还可以使用一个没有包含标记的“MyDomain”来满足“他们的propertyTags在我的包含标记中”,因此您也应该留下来加入它 试试这个
LEFT JOIN o.exclusionTags as exclusions
LEFT JOIN o.inclusionTags as inclusions
然后,您应该能够简单地执行以下操作:
AND (
exclusions NOT in (:propertyTags)
)
编辑:
好的,我有它-问题是它不在。Hibernate对此很奇怪。在我发布原始答案之前,我没有意识到所有的标签都是可为空的false,所以做左连接是没有意义的
MyDomain.executeQuery("select distinct m from MyDomain m join m.propertyTags as pt join m.inclusionTags as it where m.id != :id and m.isActive = true and (pt in (:inclusionTags) or it in (:propertyTags)) and m not in (select m from MyDomain m join m.propertyTags as pt where pt in (:exclusionTags)) and m not in (select m from MyDomain m join m.exclusionTags as et where et in (:propertyTags))", [id: id, inclusionTags: theInclusions, propertyTags: thePropertyTags, exclusionTags: theExclusions])
domain类是什么样子?@dmahapatro添加了domainI,并用一个左连接示例更新了这个问题,但是如果排除被加入到QueryTank中,它仍然不会返回任何结果。为了您的帮助,今天早些时候我实际上找到了相同的解决方案。不管怎样,赏金是你的,谢谢!
AND (
exclusions NOT in (:propertyTags)
)
MyDomain.executeQuery("select distinct m from MyDomain m join m.propertyTags as pt join m.inclusionTags as it where m.id != :id and m.isActive = true and (pt in (:inclusionTags) or it in (:propertyTags)) and m not in (select m from MyDomain m join m.propertyTags as pt where pt in (:exclusionTags)) and m not in (select m from MyDomain m join m.exclusionTags as et where et in (:propertyTags))", [id: id, inclusionTags: theInclusions, propertyTags: thePropertyTags, exclusionTags: theExclusions])