I'm new to JPA, and I'm struggling with understanding how t1.joinCollection(...) and t1.fetch(...) works together.
Assume the following entities :
Track -> [one to many] -> AudioFile
I am able to fetch the collection, but I'd also like to know how I could modify the join with e.g. additional conditions. However, the fetch and joinCollection seems to work independently, and both adds their own joins. So I'm left with two joins.
What is the right way to eagerly fetch collections, and still have the chance to add conditions to the join which is responsible for loading the collection. I've done this with the Hibernate criteria api, but I can't find my way around that in the JPA API.
CriteriaQuery<Track> cq0 = cb.createQuery(Track.class);
Root<Track> t0 = cq0.from(Track.class);
cq0.select(t0);
cq0.where(t0.get("id").in(cids));
TypedQuery<Track> q0 = entityManager.createQuery(cq0);
List<Track> tracks = q0.getResultList();
CriteriaQuery<Track> cq1 = cb.createQuery(Track.class);
Root<Track> t1 = cq1.from(Track.class);
Join<Track, AudioFile> join = t1.joinCollection("audioFiles", JoinType.LEFT);
t1.fetch("audioFiles", JoinType.LEFT);
cq1.select(t1);
cq1.where(t1.get("cid").in(cids));
TypedQuery<Track> q1 = entityManager.createQuery(cq1);
q1.getResultList();