2

Lets say we have 2 mapped and related entities: Foo -> Bar. Foo holds the FK to the Bar. The point is, that using JPA Criteria Api I need to INNER JOIN these tables and fetch all Bar entities. How can I achieve that without mapping declaring reverse relation?

kazanaki
  • 7,988
  • 8
  • 52
  • 79
Antoniossss
  • 31,590
  • 6
  • 57
  • 99

1 Answers1

1

@AVolpe thanks for pointing out select

Now thats how to do it:

CriteriaQuery<Bar> query = b.createQuery(Bar.class);

Root<Foo> root = query.from(Foo.class);

query.where(b.equal(root.get("bar").get("id"),"10219431"));
Selection<? extends Bar> join = root.join("Bar",JoinType.INNER);
query.select(join);
Bar b=getEntityManager().createQuery(query).getResultList().get(0);

This query gets Bar after INNER JOIN with Foo by FK stored in Foo (Bar is property of Foo)

Antoniossss
  • 31,590
  • 6
  • 57
  • 99