加入获取嵌套集合

时间:2020-11-02 08:50:52

标签: java hibernate jpa hql

我的实体结构如下:

E1
  List<E2> e2s; //OneToMany relation

E2
  E3 e3;
    E4 e4
      List<E5> e5s; //OneToMany relation

默认情况下,所有集合都是延迟加载的。我想定义一个JPA查询,以期获取位于E1较深级别的E5列表。

我尝试了以下查询:

"SELECT e1 FROM E1 e1" +
            " LEFT JOIN FETCH e1.e2s AS e2s" +
            " JOIN e2s.e3 AS e3" +
            " LEFT JOIN FETCH e3.e4.e5s";

"SELECT e1 FROM E1 e1" +
            " LEFT JOIN FETCH e1.e2s AS e2s" +
            " LEFT JOIN FETCH e2s.e3.e4.e5s";

导致query specified join fetching, but the owner of the fetched association was not present in the select list错误。

"SELECT e1 FROM E1 e1" +
            " LEFT JOIN FETCH e1.e2s.e3.e4.e5s";

导致illegal attempt to dereference collection [e1.e2s]错误

"SELECT e1 FROM E1 e1" +
            " LEFT JOIN FETCH e1.e2s AS e2s" +
            " JOIN e2s.e3 AS e3" +
            " JOIN e3.e4.e5s";

这不会初始化e5s集合。

使用Hibernate JPA 2.1

谢谢。

0 个答案:

没有答案