Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

What is N+1 SELECT Problem in Hibernate? Strategies to avoid this?

What Is N+1 SELECT Problem In Hibernate? Strategies To Avoid This?
In some cases while using Hibernate for object-relational mapping, you will encounter the N+ 1 select problem. That is, Hibernate will execute (n+1) queries to populate a list of records of size n, I mean 5 SELECT queries will be required to populate 4 records. This happens when each object contains reference of another object which needs to be populated. For example, suppose you have list of authors and you want to print out title of books written by each other. A naive ORM will first do a SELECT query to load all authors and then execute another N SELECT query to load book for each author. That's your N + 1 SELECT problem because it involves N+1 database roundtrips'. There are some mechanisms suggested by Hibernate that can be used to mitigate this risk e.g. EAGER fetching mode.


This post first appeared on Javarevisited: Blog About Java Programmin, please read the originial post: here

Share the post

What is N+1 SELECT Problem in Hibernate? Strategies to avoid this?

×

Subscribe to Javarevisited: Blog About Java Programmin

Get updates delivered right to your inbox!

Thank you for your subscription

×