`
kingsleylong
  • 浏览: 54946 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

[转]用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
分享到:
评论

相关推荐

    Hibernate_query查询数据表中部分字段.

    上传的资料都是非常经典的,这也是我学习过程中的心得,希望大家能给我指点,也希望大家多上传资料共同学习。千万不要传空文件夹。。。

    hibernate3中通过nativesql或取部分字段并映射为具体对象的实现

    博文链接:https://bicashy.iteye.com/blog/135925

    Hibernate识别数据库特有字段实例详解

    Hibernate识别数据库特有字段实例详解 前言: Hibernate已经为绝大多数常用的数据库数据类型提供了内置支持,但对于某些数据库的专属字段支持就不够好了。 这些特殊数据类型往往提供了比常规数据类型更好的数据表达...

    Hibernate中使用HQLQuery查询全部数据和部分数据的方法实例

    今天小编就为大家分享一篇关于Hibernate中使用HQLQuery查询全部数据和部分数据的方法实例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

    Hibernate3.1_学习源码

    02 02Hibernate_UseMyEclipse: 利用工具来配置使用Hibernate,注意要修改的内容部分。 03 03Hibernate_Component : 实体细粒度的划分,数据库中的一张表在程序中划分为几个部分的实体,配置文件和增、删、改、查的...

    Hibernate+中文文档

    1.2. 第一部分 - 第一个Hibernate应用程序 1.2.1. 第一个class 1.2.2. 映射文件 1.2.3. Hibernate配置 1.2.4. 用Ant构建 1.2.5. 启动和辅助类 1.2.6. 加载并存储对象 1.3. 第二部分 - 关联映射 1.3.1. ...

    深入浅出Hibernate中文版 part1

    第2部分 Hibernate 第2章 Hibernate概述 第3章 快速起步 3.1 准备工作 3.2 Hibernate配置 3.3 日志配置 3.4 第一段代码 3.5 代码初解 第4章 Hibernate基础 4.1 Hibernate基础语义 4.2 基础配置 4.3 ...

    hibernate3.2中文文档(chm格式)

    1.2. 第一部分 - 第一个Hibernate应用程序 1.2.1. 第一个class 1.2.2. 映射文件 1.2.3. Hibernate配置 1.2.4. 用Ant构建 1.2.5. 启动和辅助类 1.2.6. 加载并存储对象 1.3. 第二部分 - 关联映射 1.3.1. ...

    HibernateAPI中文版.chm

    1.2. 第一部分 - 第一个Hibernate应用程序 1.2.1. 第一个class 1.2.2. 映射文件 1.2.3. Hibernate配置 1.2.4. 用Ant构建 1.2.5. 启动和辅助类 1.2.6. 加载并存储对象 1.3. 第二部分 - 关联映射 1.3.1. ...

    Struts+Hibernate+Spring整合分页实例(带排序功能)part1

    这是Java常用框架阶段在为学生答疑时做的一个SSH整合分页实例(第一部分),涉及了SSH整合、日期模糊查询、查询条件绑定、查询结果排序等功能,希望能为大家提供一些思路。 分页是绝大多数MIS系统必需的功能之一,...

    Hibernate 中文 html 帮助文档

    1.2. 第一部分 - 第一个Hibernate应用程序 1.2.1. 第一个class 1.2.2. 映射文件 1.2.3. Hibernate配置 1.2.4. 用Ant构建 1.2.5. 启动和辅助类 1.2.6. 加载并存储对象 1.3. 第二部分 - 关联映射 1.3.1. ...

    Hibernate中文详细学习文档

    1.2. 第一部分 - 第一个Hibernate应用程序 1.2.1. 第一个class 1.2.2. 映射文件 1.2.3. Hibernate配置 1.2.4. 用Ant构建 1.2.5. 启动和辅助类 1.2.6. 加载并存储对象 1.3. 第二部分 - 关联映射 1.3.1. ...

    hibernate 体系结构与配置 参考文档(html)

    第一部分 - 第一个Hibernate应用程序 1.2.1. 第一个class 1.2.2. 映射文件 1.2.3. Hibernate配置 1.2.4. 用Ant构建 1.2.5. 启动和辅助类 1.2.6. 加载并存储对象 1.3. 第二部分 - 关联映射 1.3.1. 映射...

    Hibernate教程

    2.2. 第一部分 - 第一个Hibernate程序 2.2.1. 第一个class 2.2.2. 映射文件 2.2.3. Hibernate配置 2.2.4. 用Ant编译 2.2.5. 安装和帮助 2.2.6. 加载并存储对象 2.3. 第二部分 - 关联映射 2.3.1. 映射...

    Hibernate_3.2.0_符合Java习惯的关系数据库持久化

    1.2. 第一部分 - 第一个Hibernate应用程序 1.2.1. 第一个class 1.2.2. 映射文件 1.2.3. Hibernate配置 1.2.4. 用Ant构建 1.2.5. 启动和辅助类 1.2.6. 加载并存储对象 1.3. 第二部分 - 关联映射 1.3.1. ...

    深入浅出Hibernate中文版 part2

    第2部分 Hibernate 第2章 Hibernate概述 第3章 快速起步 3.1 准备工作 3.2 Hibernate配置 3.3 日志配置 3.4 第一段代码 3.5 代码初解 第4章 Hibernate基础 4.1 Hibernate基础语义 4.2 基础配置 4.3 ...

    Mybatis_day01.pdf

    说得更深入一些,如果有个查询要关联多张表,比如5张表,10张表时,而且,我们要取的字段只是其中几张表的部分字段。这时用hibernate时就会显得非常力不从心。就算用hibernate的sqlquery,后续的维护工作也会让人...

    MyBatis第一天课堂笔记.docx

    说得更深入一些,如果有个查询要关联多张表,比如5张表,10张表时,而且,我们要取的字段只是其中几张表的部分字段。这时用hibernate时就会显得非常力不从心。就算用hibernate的sqlquery,后续的维护工作也会让人...

Global site tag (gtag.js) - Google Analytics