# Join fetch on nested collections

**URL:** <https://discourse.hibernate.org/t/join-fetch-on-nested-collections/4714>\
**Category:** Hibernate ORM\
**Created:** [November 2, 2020, 11:39am UTC](https://discourse.hibernate.org/t/join-fetch-on-nested-collections/4714 "2020-11-02T11:39:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Abdullah\_Meltem](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/abdullah_meltem/32/1258_2.png) [@Abdullah\_Meltem](https://discourse.hibernate.org/u/Abdullah_Meltem)\
**Post date:** [November 2, 2020, 11:39am UTC](https://discourse.hibernate.org/t/join-fetch-on-nested-collections/4714/1 "2020-11-02T11:39:06Z")

</div>

Hi,

I have an entity structure as follows:

```
E1
  List<E2> e2s; //OneToMany relation

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

```

All the collections are lazy loaded by default. I’d like to define a JPA query to eager fetch the E5 list located in deeper levels of E1.

I tried queries like:

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

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

```

Results in `query specified join fetching, but the owner of the fetched association was not present in the select list` error.

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

```

This does not initialize the `e5s` collection.

Using `Hibernate JPA 2.1`.

Thanks in advance.

PS: The question is also available on Stackoverflow: [https://stackoverflow.com/questions/64642399/join-fetch-for-nested-collections](https://stackoverflow.com/questions/64642399/join-fetch-for-nested-collections)

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [November 4, 2020, 4:44pm UTC](https://discourse.hibernate.org/t/join-fetch-on-nested-collections/4714/2 "2020-11-04T16:44:33Z")

</div>

You need to join fetch every association individually like this:

```
SELECT e1 
FROM E1 e1
LEFT JOIN FETCH e1.e2s AS e2Alias
JOIN FETCH e2Alias.e3 AS e3Alias
JOIN FETCH e3Alias.e4 AS e4Alias
JOIN FETCH e4Alias.e5s AS e5Alias
```
