1

I have a problem. how can i return map as query in jpa ?

Map<string, User> userMap = em.createQuery("select new map(u.id, *) 
from user u"

I want to return map as query but I don't know how to.
and I don`t want to return map using to iterate list. like as the below
How to return HashMap from JPA query?
can someone help me ? in advance thank you.

ansatgol
  • 155
  • 4
  • 13
  • You can't. You can return keys/values and then put them into a Map yourself ... aka trivial – Neil Stockton Aug 31 '17 at 17:04
  • Possible duplicate of [With Hibernate, how can I query a table and return a hashmap with key value pair id>name?](https://stackoverflow.com/questions/4736743/with-hibernate-how-can-i-query-a-table-and-return-a-hashmap-with-key-value-pair) – Amogh Aug 31 '17 at 18:10
  • You are doing correct, just this HQL will return List of map For Eg. `List>` – Amogh Aug 31 '17 at 18:15
  • @NeilStockton Thakyou for answer. By the way Is this not possible in hibernate, not in jpa? – ansatgol Sep 01 '17 at 01:35
  • @Amogh Thank you for your answer. Can this be used as a list only? is this imposible to use hashmap ? – ansatgol Sep 01 '17 at 01:41
  • I guess yes, either in only list otherwise list of map – Amogh Sep 01 '17 at 01:44
  • If you are using java 8 then you can conver List to Map. [More](https://www.mkyong.com/java8/java-8-convert-list-to-map/) – Amogh Sep 01 '17 at 01:48
  • @Amogh can you see url address is example ? – ansatgol Sep 01 '17 at 03:24

1 Answers1

1

You have to make a map list at least, which means list cannot be mapped to single map.

Jake Kim
  • 112
  • 7