FETCH JOIN maximum depth?

W tried to connect to three levels:

JOIN FETCH entity1.collection1.collection2 // two OneToMany relations 

but received:

 org.hibernate.HibernateException: Errors in named queries: [...] 

Is it because it is too deep or because the collection of collections cannot be selected this way? My maximum sampling depth is 3, if relevant.

At the same time, I can make a triple FETCH JOINT, starting from the other side:

 JOIN FETCH entity3.entity2.entity1 // two ManyToOne relations 

Somehow I cannot find anything in the JPA specification or in the Hibernate docs that would limit the depth of this sentence.

+4
source share
1 answer

collection1 is of type Collection . And Collection does not have a collection2 field. This is how I reason about these types of queries.

You must create an explicit join over the collection:

 select e from Entity1 e left join fetch e.collection1 as c1 left join fetch c1.collection2 as c2 

Please note that this will result in a Cartesian product and thus will return a huge number of rows. Also note that this will only be possible if one of the two collections is at least a plurality. If both of them are bags, Hibernate throws an exception when the request is executed.

+4
source

Source: https://habr.com/ru/post/1397786/


All Articles