【问题标题】:Grails: HasMany Intersection QueryGrails:HasMany 交集查询
【发布时间】:2013-10-08 15:50:10
【问题描述】:

给定以下域结构:

Book {
    static hasMany = [tags: Tag]
}

Tag {
    String name
}

我正在尝试找到一种方法,在给定Book 的情况下,我可以找到包含这本书的任何标签的任何其他书籍。

我试过了:

Book.findAllByTagsInList(myBook.tags)

但正如预期的那样,“列表中的列表”查询没有产生所需的结果。

【问题讨论】:

    标签: sql grails grails-orm


    【解决方案1】:

    您可以使用标准作为

    def books = Book.createCriteria().listDistinct{
        tags{
            'in'('id', myBook.tags*.id)
        }
    }
    

    或使用 HQL 作为

    def books = Book.executeQuery("select distinct b from Book as b \
                                   inner join b.tags as tag \
                                   where tag.id in (:tags)", 
                                   [tags: myBook.tags*.id])
    

    【讨论】:

      猜你喜欢
      • 2019-12-19
      • 2015-06-23
      • 1970-01-01
      • 2013-09-20
      • 1970-01-01
      • 2019-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多