The standard fetch strategy for collections in hibernate is lazy select fetching (hibernate performance). So if you retrieve some entities (for example via a query or a collection association) with a lazy collection association, subsequent calls to this lazy association will be retrieved one by one in further requests, for example. This is called the N+1
-query problem, since there is one query for retrieving the entities itself and N
additional queries for the lazy association. You can reduce the overhead by setting the hibernate.default_batch_fetch_size
for your persistence unit in the persistence.xml
. This reduces the problem to an N/batch_size +1
-query problem. You can tell hibernate to use only 1+1
-queries, if you set the fetch strategy to subselect fetching. This way, all entities in the association collections of the entities are retrieved in one query as soon as one of the associations is accessed. This will be helpfull only, if you do not pick one of the objects and retrieve the association, but iterate through your results and access the association of each of them. It is even contra productive otherwise. Read More