Wednesday, 18 September 2013

How do I return a list of joined hibernate entities?

How do I return a list of joined hibernate entities?

Suppose I have a MySQL table, and entity tag in hibernate, where a tag is
a row in the following
id (primary key), tag, entity id
1, food, 77
2, shop, 98
3, food, 32
...
I'd like to return the total counts of the same tag sorted in decreasing
number of entries. Aka,
tag, count
food, 2
shop, 1
...
I was told to do something like
SELECT tag, COUNT(*) `count`
FROM table1
GROUP BY tag
ORDER BY `count` DESC
Output:
| TAG | COUNT |
|------|-------|
| food | 2 |
| shop | 1 |
However, how do I read this newly created list of entities out from
Hibernate? Do I need to somehow define a new object to read this list out?
The same question goes for say reading an entity (row) that is defined,
but just joined with another entity as part of a query. How do I read out
the result?
Thanks!

No comments:

Post a Comment