Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get java.util.Map from hibernate query?

Tags:

java

hibernate

I am wondering what is the best way to get map array from hibernate query. Google says to iterate query.list(), and create/put objects into empty map array.
I guess there would be some elegant and efficient way to do this. Could somebody give me idea?

like image 868
WSK Avatar asked Jul 20 '10 17:07

WSK


1 Answers

See Hibernate Documentation - 15.6. The select clause:

You can assign aliases to selected expressions using as:

select max(bodyWeight) as max, min(bodyWeight) as min, count(*) as n
from Cat cat

This is most useful when used together with select new map:

select new map( max(bodyWeight) as max, min(bodyWeight) as min, count(*) as n )
from Cat cat

This query returns a Map from aliases to selected values.

like image 177
uı6ʎɹnɯ ꞁəıuɐp Avatar answered Oct 13 '22 00:10

uı6ʎɹnɯ ꞁəıuɐp