Search code examples
hibernatehql

Hibernate Query Language (HQL) - Queries for lazy/no-lazy loading


I have a structure like this:

  • A contains collection of B (mapped as not-lazy)
  • B contains collection of C (mapped as not-lazy)

I'd like to make a query, that retrieves A objects, that contain B objects without the C objects within them. Is that possible? The other way around will work for me too (if B-C relation is mapped lazy and the query retrieves A, containing B and C).

Thanks!


Solution

  • No, it's not possible. Since you marked the association itself as eagerly-loaded, Hibernate will always load this association eagerly.

    If you mark the association as lazy (the default for toMany associations), then you have th option of eagerly fetching them in a query, using a 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 of the collections are bags (i.e. Lists without index column).