Alfredo Osorio Alfredo Osorio -4 years ago 127
Java Question

JPA 2 Criteria Fetch Path Navigation

With JPA 2 Criteria Join method I can do the following:

//Join Example (default inner join)
int age = 25;
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Team> c = cb.createQuery(Team.class);
Root<Team> t = c.from(Team.class);
Join<Team, Player> p = t.join(Team_.players);, age));
TypedQuery<Team> q = entityManager.createQuery(c);
List<Team> result = q.getResultList();

How can I do the same with fetch method, I expected that Fetch interface had get method for path navigation but it doesn't:

//Fetch Join Example

int age = 25;
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Team> cq = cb.createQuery(Team.class);
Root<Team> t = cq.from(Team.class);
Fetch<Team,Player> p = t.fetch(Team_.players);
cq.where(cb.equal(p.get(Player_.age), age)); //This leads to compilation error there is no such method get in interface Fetch
TypedQuery<Team> q = entityManager.createQuery(cq);
List<Team> result = q.getResultList();

According to Hiberante Documentation fetch returns a Join object which is wrong.

Answer Source

Agree with you about that method, and the fact that you would expect it to allow what you say. Another option would be

Join<Team, Player> p = t.join(Team_.players);
t.fetch(Team_.players);, age));

i.e do a join(), add a fetch() for it, and then make use of the join. This is illogical and only adds to the inelegant nature of JPA Criteria, but anyway, ought to be a workaround

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download