JPA specification allows up to two simultaneous eager fetches. But sometimes when we really need to this – we are stuck. One way to achieve this is to use hibernate extensions.
We could do something like this:
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "keys")
@Fetch(value = FetchMode.SUBSELECT)
private List<T> keys;
@Fetch(value = FetchMode.SUBSELECT)
private List<T> keys;
Second line instruct hibernate to fetch this collection eagerly even if there are already two such declarations. Remember however that this is an expensive operation and it should be avoided whenever possible. Good strategy is to always fetch (potentially big) bags lazily. Fetch owning entity via named queries that will provided desired collection only when needed.