HQL:多个JOIN集合查询返回Nothing

时间:2013-10-28 15:26:50

标签: java grails hql

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

  

他们的财产标签在我的包含标签中   或者我的财产标签是他们的包含标签,
  和他们的财产标签不在我的排除标签,
  和我的财产标签不在他们的排除标签。

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

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

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

1 个答案:

答案 0 :(得分:0)

我看到的第一个问题是您正在对排除标记进行内部联接。这将确保它只返回具有排除标记的记录。你也可以用“MyDomain”来满足“他们的属性标签在我的包含标签中”,它没有包含标签所以你也应该加入那个

试试这个

LEFT JOIN o.exclusionTags as exclusions
LEFT JOIN o.inclusionTags as inclusions

然后你应该能够做到这一点:

AND (
    exclusions NOT in (:propertyTags)
)

修改: 好吧,我有它 - 问题是没有.Hibernate对此很奇怪。在我发布原始答案之前,我没有意识到所有标签都是可以为空的,所以做左连接是没有意义的。

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