Query#list()returns a list of object arrays. Each field of the array contains one of the selected properties.
1.
select c.name, c.order
2.
from Customer c
In my opinion iterating over lists of
Object[] is verbose and error-prone because processing the result-list is directly dependant on the sequence of selected properties in the hql string. If you change the query string you have to change the sourcecode as well.
Did I mention how expressive HQL is? :) Check out the following snippet:
1.
select new map(c.name as name, c.order as order)
2.
from Customer c
Instead of returning arrays this query will return a list of maps each containing entries with aliases (the keys) to selected values.
Actually it's possible to let Hibernate return partially loaded entities instead of maps containing the properties of interest. Check out the following example:
1.
select new Customer(c.name, c.order)
2.
from Customer c

本文介绍如何使用Hibernate查询多个属性而非整个对象的方法。通过HQL可以灵活选择返回数组、映射或部分加载实体,提供了多种实用的查询技巧。
988

被折叠的 条评论
为什么被折叠?



