Hibernate fetching strategy – when to use “join” and when to use “select”?

Join is supposed to solve the n+1 problem. If you have 10 parents, each with 10 children, join will require one query and select will require 11 (one for the parents and one for the children of each parent). This may not be a big deal if the database is on the same server as the application or if the network is really fast, but if there is latency in each database call, it can add up. The join method is a little less efficient on the initial query because you’re duplicating the parent columns in every row, but you only make one round-trip to the database.

Generally, if I know I’m going to need the children of all the parents, I go with join. If I’m only going to need the children of a few parents, I use select.

Leave a Comment