1
假設我有一個JPA實體和查詢:收藏獲取加入
@Entity
public class MyEntity {
@OneToMany(fetch = FetchType.LAZY)
private List<ChildEntity> children = new ArrayList<ChildEntity>();
}
public List<MyEntity> fetchAll() {
return em.createQuery("select distinct e from MyEntity e join fetch e.children")
.getResultList();
}
沒有不同關鍵字會做myEntity所和e.children的跨產品。
使用獨立和聯合提取避免N + 1選擇合併問題是否被認爲是一種好的做法?它有副作用嗎?
不,它有同樣的結果。假設我有兩個MyEntities和兩個孩子。此查詢返回4個MyEntities,但我只需要2個。 – ike3