7

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?

WSK
  • 5,949
  • 8
  • 49
  • 74
  • What would be the keys and what would be the values of this Map? Any attempt to answer this question would require a heck of a lot more information about what you'd like to have happen. – matt b Jul 20 '10 at 18:43
  • possible duplicate of [Can Hibernate return a collection of result objects OTHER than a List?](http://stackoverflow.com/questions/416970/can-hibernate-return-a-collection-of-result-objects-other-than-a-list) – Pascal Thivent Jul 20 '10 at 19:03

1 Answers1

6

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.

uı6ʎɹnɯ ꞁəıuɐp
  • 3,431
  • 3
  • 40
  • 49
  • 2
    if you execute the hql using createQuery.list it will just return a list. Each element of the list is a map with one key & one value. So it is not returning a map it is returning a list of maps...am I missing something... – hba Feb 12 '13 at 01:20