I have a structure like this:
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!
In Hibernate, there are two main components which can be lazy loaded. They are namely Entities and Collections. An Entity represents a relational data in database, whereas a collection represents collection of children for an entity.
You can select only certain columns with HQL, too, for example you can use select column from table in HQL. Native SQL is not necessarily faster than HQL. HQL finally also is translated into SQL (you can see the generated statement when running the application with the show_sql property set to true).
Hibernate Query Language (HQL) is an object-oriented query language, similar to SQL, but instead of operating on tables and columns, HQL works with persistent objects and their properties. HQL queries are translated by Hibernate into conventional SQL queries, which in turns perform action on database.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With