Home / Java Techs / Grails / Unveil magic of hibernate lazy loading

Unveil magic of hibernate lazy loading

Print Friendly

Recently we have discussed about the performance of retrieving collections/relations/associations inside objects. By the way, we also review the machinery that hibernate handles these relational concepts to improve the effectiveness of database operations. One of the techniques being implemented in Hibernate is lazy loading. In this post, I want to share you what I have learned how hibernate lazy loading works with a toy application.

Design the application

Here are our domain classes

To test the application simply, we will try to create some toy records in bootstrap.groovy  as below:

In order to look at exactly when hibernate will invoke SQL queries, we can either turn on SQL logging in DataSource.groovy  or enable the general query log. I suggest that we should use the former for simplicity.

Now let’s create a controller to hold our test code (just for convenience, this could also be done in a groovy console):

The method just retrieves the author named ‘Ethan’ that has been created in BootStrap, then collect all his books and print out their name.

Have you ever wondered that how many query commands could hibernate issue from just the simple method index  of TestController? It might be called at the line 6, 8 or 10. Now we are going to go through the code and analyse phenomenon could be occurred.

Forecast likely circumstances

At the line 6, we are using a dynamic finder to look up author based its name. Hibernate is likely to issue a query to get the instance of Author class at which name is ‘Ethan’.

Dynamic method that uses the properties of the domain class to create query method expressions that return all matching instances of the domain class.

See more at: http://docs.grails.org/2.5.5/ref/Domain%20Classes/findAll.html

At the line 8, we are asking for the books  field where hibernate will retrieve them. Unfortunately Grails uses lazy loading by default, so we cannot see any queries to be issued from this call. Indeed, books haven’t been touched at this point. To ensure the performance, hibernate lets them lived as soon as there are another actually operations hitting the database.

Hibernate won’t actually hit the database to retrieve the data until the line 10 because we expect to get individual book via iterating on the books collection of the given author.

The line 13 reveals how the magic happens. We expect myBooks  to be an instance of  List , but it is literally one of  PersistentSet.

Analyse SQL queries log

The output above proofs what I have explained you in the previous sentences.

This writing was given birth to based on what I had read from [1] and various experiences on real projects I am working on. Hopefully you find more interesting as well as helpful from this post. It’s more respectful to have your comments and suggestions in the comment sections.

Source codes

The toy application can be found at https://bitbucket.org/it4bus/hibernate-lazy-evaluation.


[1] Hibernate magic with lazy evaluation, accessed 25th February, 2017


About Admin

programming all time and taking a bread for the lunch hahaha... When you are engaging your work, nobody can attract you doing another thing. Otherwise, should should move to other jobs or are not interested in your job enough.