六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 44|回复: 0

让Hibernate Query返回组织好的VO对象列表

[复制链接]

升级  31.33%

23

主题

23

主题

23

主题

秀才

Rank: 2

积分
97
 楼主| 发表于 2013-1-19 04:07:45 | 显示全部楼层 |阅读模式
举例说明:
 
数据库中有表:student(studentid, studentname, age)
               表:class(classid, classname, studentid)
 
程序中有VO:StudentInfo(studentid, studentname, age, classname)   (假设StudentInfo在com.test包中)
 
在不使用hibernate做关系映射的前提下(抛弃了hibernate的精髓,做法不可取···),想通过一个HQL直接返回一个已经组织好的List<StudentInfo>,比较精辟的做法是:
 
DAO中:String sql = "select new com.test.StudentInfo(s, c.classname)";sql += " from Student s, Class c where s.studentid=c.studentid";Session session = this.getSession();Query query = session.createQuery(sql);return query.list();  
StudentInfo中肯定要做一下准备,写一个合适的构造方法:
 
public StudentInfo(Student s, String classname) {    this.studentid = s.studentid;    this.studentname = s.studentname;    this.age = s.age;    this.classname = classname;} 这样就可以了。
 
另外:
上面HQL的做法详见:hibernate文档的[14.4 select子句]
 
 
 
 
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表