10

In Grails / GORM, what is the difference between static mapping = {xyz lazy: false} & static fetchMode = [xyz: 'eager']?

Example:

class Book {
    static belongsTo = [author: Author]
    static mapping   = {author  lazy: false}
    static fetchMode = [author: 'eager']
}
XDR
  • 4,070
  • 3
  • 30
  • 54
  • I think this might answer your question: [http://stackoverflow.com/questions/654704/grails-gorm-default-fetch-strategy-when-to-set-fetchmode-to-eager-eager-vs][1] [1]: http://stackoverflow.com/questions/654704/grails-gorm-default-fetch-strategy-when-to-set-fetchmode-to-eager-eager-vs – David Chavez May 06 '15 at 13:53
  • 2
    Thanks for the link, but the answer seems to describe the difference between `lazy: true` & `lazy: false`. The answer doesn't mention `fetchMode = [...]` – XDR May 07 '15 at 04:35
  • It would be nice it someone could answer this question as I didn't find any explanation on the grails documentation neither on the internet. – Merlin Oct 19 '15 at 09:17

1 Answers1

9

The difference between lazy:false and fetchMode 'eager' are

  1. lazy:false will get the associated domain object by querying again to database using Select Query, but fetchMode 'eager' which is deprecated now(use fetch:'join') will try to join the associated tables(using outer join) and fetch the associated objects in single query.
  2. lazy:false will have one more query to the database to fetch the associated domain object and hence would be having more interactions with the database whereas fetch:'join' will have less interaction to fetch the same data.
  3. FetchMode Join overrides the lazy property. It will simple ignore the lazy:false.

Should you be interested in a detailed explanation about Fetchmodes, take a look http://www.solidsyntax.be/2013/10/17/fetching-collections-hibernate/. The article describes the Hibernate fetchmodes and the output which they produce.

Hope this helps.

Anshul
  • 687
  • 9
  • 18