I have a one-to-many mapping between the parent entity and the child objects. Now I need to find the number of children associated with each parent for the list of parents. I am trying to do this using HQL, but I'm not sure how I can get the list of parents. In addition, I do not know how I can return the object itself, and not just its identifier. My current HQL query:
select new map(parent.id as parentId, count(*) as childCount) from Parent parent left join parent.children children group by parent.id
but this only returns an identifier and does not filter specific parents.
EDIT Based on Pascal's answer, I changed the request to
select new map(parent as parent, count(elements(parent.children)) as childCount) from Parent parent group by parent
This works, but is excessively slow: 30 seconds instead of 400 ms in one database.
java orm hibernate hql
Thomas LΓΆtzer
source share