![](/img/trans.png)
[英]JPA Criteria query eager fetch associated entities using a SINGLE query with joins instead of multiple queries
[英]JPA Criteria - Fetch child entities instead of root
假設我們有2個映射的實體和相關實體: Foo
> Bar
。 Foo
持有FK到大Bar
。 關鍵是,使用JPA Criteria Api
我需要INNER JOIN
這些表並獲取所有Bar
實體。 在沒有映射聲明反向關系的情況下如何實現?
@AVolpe感謝您指出select
現在多數民眾贊成在如何做到這一點:
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);
該查詢在存儲在Foo
的FK的Foo
的INNER JOIN
之后獲得Bar
( Bar
是Foo
屬性)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.