[转]用Hibernate查询部分字段

How to query several columns with Hibernate?

Even though Hibernate is called an  object-relational mapper it's easy to select only a few properties instead of the whole mapped object. The hibernate query language (HQL) is very expressive. If you select some properties instead of the whole object, the call of  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

This query will return lists of customers assuming there is an appropriate constructor.

You should really pay attention with this approach of partially loaded entities because updating these entities could lead to accidently loosing data.
From: http://bwinterberg.blogspot.hk/2009/08/how-to-query-several-properties-with.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值