Grails、Gorm,使用 createCriteria 或其他一些替代方法

Grails,Gorm, using createCriteria or some other alternative

比如我parentclass作者:

class Author {    
    String name
    static hasMany = [
         fiction: Book, 
         nonFiction: Book
    ]
}

和一本childclass书:

class Book {    
    String title
    static belongsTo = [author: Author]
}

我已经为作者做了一些记录:

def fictBook = new Book(title: "IT")
def nonFictBook = new Book(title: "On Writing: A Memoir of the Craft")
def a = new Author(name: "Stephen King")
             .addToFiction(fictBook)
             .addToNonFiction(nonFictBook)
             .save()

如何找到 parent 的 child-class 记录和 child 的 parent-class 记录?

我试过使用findBy方法,如下:

def book = Book.get(1)
def author = Author.findByFiction(book)

但是我得到一个错误:

Parameter "#2" is not set; SQL statement:

我读到,在某些关系中 findBy 被拒绝使用,我如何使用条件或其他一些方法重写它?

当你添加这个时belongsTo

static belongsTo = [author: Author]

这会触发添加一个名为 author 且类型为 Author 的 属性(通过编译期间的 AST 转换),实际上类似于声明

Author author

但不要那样做,那是多余的。

因此,如果您有一本书,可以通过 属性:

访问 Author
def book = Book.get(1)
def author = book.author