I'm working with hibernate and I'm having troubles creating an hql query that fetches all the children of my object.
For example: The Object User has a list of Cars and a list of Friends.
To get a user with his cars I would use following query:
from User u left join fetch u.cars where u.id = ?
This works fine, so I thought it would be easy to get a user with his cars and with his friends with following query:
from User u left join fetch u.cars left join fetch u.friends where u.id = ?
But this gives me following error:
HibernateException: cannot simultaneously fetch multiple bags
Now my question is: what is the right way to fetch multiple children in hibernate?
At most one of the children collection must be a bag (i.e. declared as a List). Declare the other collections as Sets, and it will work.
Beware, though, that doing such fetch joins makes a cartesiann product of the rows. If both collections have 100 elements, such a query retrieves 10,000 rows fro the database. It's sometimes more efficient to execute a first query which fetches one collection, and a second one which fetches the other (which thus reduces the number of rows retrieved to 200). This is also a way to avoid the problem you have:
select u from User u left join fetch u.cars where u.id = :id;
select u from User u left join fetch u.friends where u.id = :id;