There are two tables, report and reportcomment. Every reportcomment is assign (via foreign key) to a report, therefore a report has zero to many reportcomments.
If I need a list, that gives me all reports with the respective number of comments of every report, I would do the following (with SQL):
SELECT r.*, c.cnt
FROM report r
LEFT JOIN (
SELECT ir.id AS report_id, COUNT(ic.id) AS cnt
FROM report ir
INNER JOIN reportcomment ic ON ir.id = ic.report_id
GROUP BY ir.id
) c ON c.report_id = r.id
I would like to retrieve such a list with JPA/Hibernate and store the c.cnt somewhere in my Report entity object.
How could that be achieved?
I think the simpliest way would be to create a transient field in
Reportand convert tuples returned by the appropriate query manully, something like this: