n+1 problem in hibernate

Let us understand n+1 problem with a simple example. Suppose we have collection of students and every student have collection subjects. To get all students and all subjects per students we need to execute one query to fetch all students and n queries to fetch subjects of n number of students. At the last we need to execute n+1 queries.

In hibernate n+1 problem occurs in case of lazy loading.

Hibernate n+1 problem solution:
We can use eager loading but it may leads to the performance issues so we have to use BasicCollectionLoader to load all children at once, when needed.

Content Protection by DMCA.com
Please Share