0

我正在尝试编写返回以下内容的 HQL 查询:

他们的propertyTags 在我的inclusionTags 中
或者我的propertyTags 在他们的inclusionTags 中,
并且他们的propertyTags 不在我的exclusionTags 中,
并且我的propertyTags 不在他们的exclusionTags 中。

这是我到目前为止所得到的:

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])

问题是无论包含/属性标签匹配,加入 excludeTags 不会返回任何内容。删除所有排除子句仍然不返回任何内容,要返回任何内容的唯一方法是完全删除 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])
4

1 回答 1

0

我看到的第一个问题是您正在对排除标签进行内部连接。这将确保它只返回具有排除标签的记录。您也可以使用没有包含标签的“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])
于 2013-10-29T02:12:50.757 回答