Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails / GORM: difference between lazy: false & fetchMode eager

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']
}
like image 299
XDR Avatar asked Apr 23 '15 09:04

XDR


People also ask

What is Grails Gorm?

GORM is the data access toolkit used by Grails and provides a rich set of APIs for accessing relational and non-relational data including implementations for Hibernate (SQL), MongoDB, Neo4j, Cassandra, an in-memory ConcurrentHashMap for testing and an automatic GraphQL schema generator.

What is Grails domain class?

A domain class fulfills the M in the Model View Controller (MVC) pattern and represents a persistent entity that is mapped onto an underlying database table. In Grails a domain is a class that lives in the grails-app/domain directory.


1 Answers

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.

like image 104
Anshul Avatar answered Sep 28 '22 05:09

Anshul