使用动态查找器时有没有办法忽略 hasMany 关系?

Is there a way to ignore a hasMany relationship when using dynamic finders?

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

class Book {
    static hasMany = [chapters: Chapter]
    String title
}

class Chapter {
     String chapter
}

如果我试图找到所有作者 Author.findAll() ,有没有办法忽略被拉入内存的章节关系?

f I am trying to find all authors Author.findAll() , is there a way to ignore the chapter relationships that are being pulled into memory as well?

是的。这就是默认情况下会发生的情况。使用域 类 完全按照您在此处显示的那样编写 Author.findAll() 将生成 SQL 像这样(确切的语法可能会有所不同,具体取决于您使用的方言,这就是将生成的内容对于 H2):

select author0_.id as id1_0_, author0_.version as version2_0_, author0_.name as name3_0_ from author author0_

如果您开始与 Author 实例交互并引用 books 属性,这将触发更多 sql 被发送到数据库。

希望对您有所帮助。