Hibernate Query Language (HQL) - queries for lazy / non-lazy loading

I have a structure like this:

  • A contains set B (displayed as non-lazy)
  • B contains set C (displayed as non-lazy)

I would like to make a query that retrieves objects A that contain objects B without objects C inside them. Is it possible? Another way would work for me too (if the BC relation appears lazy and the query retrieves A containing B and C).

Thanks!

+6
source share
1 answer

No, It is Immpossible. Since you noted that the association itself was impatiently loaded, Hibernate will always download this association impatiently.

If you mark the association as lazy (by default for toMany associations), then you have the opportunity to willingly retrieve them in the request using join fetch :

 select a from A a left join fetch a.bs b left join fetch b.cs 

Note that this will not work if both collections are bags (i.e. lists without an index column).

+5
source

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


All Articles