0

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" +
            " 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";

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.e3.e4.e5s";

Results in illegal attempt to dereference collection [e1.e2s] 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.

suat
  • 4,239
  • 3
  • 28
  • 51
  • Does this answer your question? [query specified join fetching, but the owner of the fetched association was not present in the select list](https://stackoverflow.com/questions/12459779/query-specified-join-fetching-but-the-owner-of-the-fetched-association-was-not) – SternK Nov 02 '20 at 08:57
  • No, it does not. I've added an additional case that I tried. It did not initialize the collection that I'm expecting to be initialized. – suat Nov 02 '20 at 09:17
  • I have never seen you explicitly do `JOIN e3.e4 AS e4`, is that correct? – Smutje Nov 02 '20 at 09:26
  • Yes, that's correct. But there are other queries that I chain the fields without such explicit joins like `e2s.e3.e4.e5`. But never used such phrases in a `JOIN FETCH` – suat Nov 02 '20 at 09:30
  • `e1.e2s` is a collection, not an E2, so you can't do `e1.e2s.e3`, it would make no sense. – Maurice Perry Nov 02 '20 at 09:34
  • @MauricePerry, yes, actually, you're right. Just tried them out. In 1st and 4th cases, I tried to get a reference to `E2` instances by an explicit join but still could not initialize the `E5` list. – suat Nov 02 '20 at 09:39

0 Answers0