Scenario
Same scenario again, this time we will load all chapters in one step.
Solution
The property subselect fetching defines that Hibernate should load all chapters of all books, when the first collection is being accessed.
@OneToMany(cascade = CascadeType.ALL) @JoinColumn(nullable = false) @Fetch(FetchMode.SUBSELECT) private Set<Chapter> chapters = new HashSet<Chapter>();
This is slightly different than the batch size, which loads only chapters for a couple of books. You might consider to use batch fetching if you want to load the chapters only for a couple of books, where as subselect fetching is useful if you always want to load the chapters for all books of the result list. There are less use cases for subselect fetching as it is to aggressive for most situations.
You can play around with subselect fetching in the method efficientBatchSizeForRelation in the class PerformanceTest. Just uncomment the @Fetch annotation in the book class.