Return a subset of a JPA entity as a array of maps from a JPQL query?

一世执手 提交于 2020-01-12 20:57:09

问题


In JPQL it is possible to ask for a subset of an entity using a constructor expression such as

SELECT NEW example.EmployeeDetails(e.name, e.salary, e.department.name) FROM Employee e

which returns a list of objects of type EmployeeDetails

or using a projection select such as

SELECT e.name, e.salary FROM Employee e

which returns an Object[] result where result[0] is e.name and result[1] is e.salary

is there a way to get JPA to return a Map which contains a subset of the entity for example is there a JPQL query that can return List<Map<String,Object>> result such that result.get(0).get("e.name") return e.name and result.get(0).get('e.salary') return e.salary

If JPQL can't do it does can HQL do it?


回答1:


JPA provides limited amount of return types for compound selection: array, tuple and construct, while Hibernate provides much more return types for select clause, which includes Map.

SELECT NEW map(e.name, e.salary, e.department.name) FROM Employee e

This HQL query returns a Map from aliases to selected values.



来源:https://stackoverflow.com/questions/12076238/return-a-subset-of-a-jpa-entity-as-a-array-of-maps-from-a-jpql-query

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!