次を返す HQL クエリを作成しようとしています。
彼らの propertyTags が my includeTags に含まれている、
または my propertyTags がその includeTags に含まれており、
かつ彼らの propertyTags が my excludeTags に含まれておらず、
かつ my propertyTags がそれらの excludeTags に含まれていない。
ここに私がこれまでに持っているものがあります:
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])
問題は、包含/プロパティタグの一致に関係なく、除外タグを結合すると何も返されないことです。すべての除外句を削除しても何も返されません。何かを返す唯一の方法は、JOIN を完全に削除することです。
ドメイン:
MyDomain {
boolean isActive = true
static hasMany = [propertyTags: Tag, inclusionTags: Tag, exclusionTags: Tag]
static constraints = {
propertyTags(nullable: false)
inclusionTags(nullable: false)
exclusionTags(nullable: false)
}
}
Tag {
private static final List<Integer> TAG_TYPES = [...]
String name
int type
String description
static constraints = {
name(unique: true, nullable: false, blank: false)
type(inList: [TAG_TYPES])
description(nullable: true, blank: true)
}
}
アップデート:
包含/除外を次のLEFT JOIN
ように変更しましたが、除外がクエリに含まれている場合、返されるレコードはまだありません:
List<MyDomain> objects = MyDomain.executeQuery("""
SELECT DISTINCT o
FROM MyDomain o
JOIN o.propertyTags as propertyTags
LEFT JOIN o.exclusionTags as exclusions
LEFT JOIN o.inclusionTags as inclusions
WHERE o.id != :id
AND o.isActive = true
AND 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])