(Quick Reference)
fetchMode
Purpose
Allows the configuration of an associations fetch strategy ('eager' or 'lazy')
Examples
class Author { String name static hasMany = [books: Book] static fetchMode = [books: 'eager']
}
In this example the
fetchMode
static property specifies that the
book
association should be fetching eagerly
Description
By default associations in Grails are fetched lazily (each record is read from the database only when it is first accessed from the collection). This makes sense for most cases, however in the case where you have a small number of records to fetch and/or are repeatedly required to load lazy associations (resulting in N+1 queries) it makes sense to use eager fetching.
In the case of eager fetching and a one-to-many association, the instance as well as the association will be initialized when they are loaded (eagerly). However, caution should be observed when using eager fetching, since being too eager can result in your entire database being loaded into memory!