Consider the following JPQL query:
SELECT foo FROM Foo foo
INNER JOIN FETCH foo.bar bar
WHERE bar.baz = :baz
I'm trying to translate this into a Criteria query. This is as far as I have gotten:
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Foo> cq = cb.createQuery(Foo.class);
Root<Foo> r = cq.from(Foo.class);
Fetch<Foo, Bar> fetch = r.fetch(Foo_.bar, JoinType.INNER);
Join<Foo, Bar> join = r.join(Foo_.bar, JoinType.INNER);
cq.where(cb.equal(join.get(Bar_.baz), value);
The obvious problem here is that I am doing the same join twice, because Fetch<Foo, Bar>
doesn't seem to have a method to get a Path
.
Is there any way to avoid having to join twice? Or do I have to stick with good old JPQL with a query as simple as that?