GORM一对多:用给定的多个过滤一个

时间:2013-08-12 04:09:51

标签: grails gorm

In this link

一对多的关系是:

    class Author {
           static hasMany = [books: Book]
           String name 
     }

如果我们在Book中指定belongsTo,即

class Book {
    String title
}

如何使用给定的书籍过滤作者。

def books=[book1,book2,book3]

2 个答案:

答案 0 :(得分:0)

您可以使用create criteria来获取作者:

    def bookList = //some book list
    def authors = Author.createCriteria().list {
        books {
            'in'('title', bookList*.title)
        }
    }*.name
    println "Authors = ${authors}"

答案 1 :(得分:0)

我更喜欢HQL。

Author.executeQuery("select a from Author a join a.books as b where b in (:bookList)", [booklist: books])