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?
Asked
Active
Viewed 2,189 times
2

kazanaki
- 7,988
- 8
- 52
- 79

Antoniossss
- 31,590
- 6
- 57
- 99
-
1check this http://stackoverflow.com/questions/9802717/using-projections-in-jpa-2 – Arturo Volpe Aug 21 '14 at 12:05
1 Answers
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