Search code examples
hibernategrailsgrails-ormdynamic-finders

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
}

If 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?


Solution

  • 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?

    Yes. That is what will happen by default. With the domain classes authored exactly as you have shown there Author.findAll() will generate SQL like this (the exact syntax may be different depending on what dialect you are using, this is what will be generated for H2):

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

    If you start interacting with the Author instances and reference the books property and that will trigger more sql being sent to the database.

    I hope that helps.