GORM fetching mechanisms and issues

Posted By : Shiv Kumar | 15-Dec-2015

Gorm provides two type of fetching mechanism using Hibernate under the hood :

1. Eager Fetching (Instant Loading)

2. Lazy Fecthing (Lazy/Late Loading) Lazy fetching is default.

 

1. Eager Fetching : Using this fetching mechanism the required instance along with their associated properties immediately loads into the memory but with separate db select query for associations(Internally).

	static mapping={
 	   propertyName lazy : false
	}
	
	Another alternative to lazy : false

	static mapping={
 	   propertyName fetch : 'join'  // It will also load the associated objects in single query by left outer join.
	}

 

Pros : It avoids extra database query to fetch other associated properties and we can directly get them from the instance immediately.

Cons : It downgrade the performance of the application as the whole data is not required immediately in all the cases.

 

2. Lazy Fetching : Using this fetching mechanism, the associated data is fetched on the demand with the help of new separate query on database.

	static mapping={
 	   propertyName lazy : true   // default
	}

 

Pros : No overloaded data every time, as the required things are fetched on demand.

Cons : There are several issues with Lazy fetching.

 1. Separate query is required to fetch the associated data.

 2. It creates issue when object is deattached from session and throws LazyInitializationException.

I have faced the same issue while working with a module. The required object lost from the hibernate session and no other property was available then.

 

Ways to solve the problem :

1. Make the fetching mode "eager" but it is costly.

2. Again attach the object to the session using either of the following approach :


	I. merge() - Again attached the object to the session but persist the current changes to the database.
		User user = User.get(1)
		....
		....
		object deattched from session
		....
		....
		user = user.merge()

	II. attach() - Attach the object to the session again but regardless of the changes in the database.
		User user = User.get(1)
		....
		....
		object deattched from session
		....
		....
		user = user.attach()

 

Using the above mentioned appraoch I am able to attach the instance to the session and it solved my problem.

Hope this will also help you.

Thanks

About Author

Author Image
Shiv Kumar

Shiv is an experienced Java Developer with a strong background in multiple technologies. He specializes in defining system architectures to ensure reliable and resilient solutions. He possesses a comprehensive understanding of the latest technologies and has hands-on experience in Core Java, Spring Boot, Hibernate, Apache Kafka messaging queue, Redis, as well as relational databases like MySQL and PostgreSQL, and non-relational databases like MongoDB. He excels in API implementations, Microservices, Web Services development, testing, and deployments. Shiv actively contributes to code enhancements and consistently delivers valuable contributions to various client projects, including Fabtrack, Pando, Pandojo, Digikam, WhatsApp Integration, Croniz, Punchin Application, Script TV, Bhaasha, and more. He demonstrates strong analytical skills and a creative mindset. In addition, he has a passion for reading books and exploring new technologies and innovations.

Request for Proposal

Name is required

Comment is required

Sending message..