一对多关系是:
class Author {
static hasMany = [books: Book]
String name
}
如果我们没有在 Book 中指定 belongsTo,即
class Book {
String title
}
如何过滤给定书籍的作者。
def books=[book1,book2,book3]
一对多关系是:
class Author {
static hasMany = [books: Book]
String name
}
如果我们没有在 Book 中指定 belongsTo,即
class Book {
String title
}
如何过滤给定书籍的作者。
def books=[book1,book2,book3]
您可以使用创建条件来获取作者:
def bookList = //some book list
def authors = Author.createCriteria().list {
books {
'in'('title', bookList*.title)
}
}*.name
println "Authors = ${authors}"
我更喜欢 HQL。
Author.executeQuery("select a from Author a join a.books as b where b in (:bookList)", [booklist: books])