Hibernate-多表关联查询结果的处理方法
Hibernate多表查询结果处理
(2014-07-06 20:45:40)
标签: hibernate多表查询结果集处理 | 分类: Java |
如果我们在Hibernate中需要查询多个表的不同字段,那么如何来获取Hibernate多表查询的结果呢?有两种方式:
1、 对各个字段分别转化成对应类型,如下:
Java代码:
Query q = session.createQuery(" select members, classInfo.className " +
" from Members members, ClassInfo classInfo " +
" where members.level = classInfo.classCode ");
List result = q.list();
Iterator it = result.iterator();
while (it.hasNext()) {
Object[] tuple = (Object[]) it.next();
Members members = (Members) tuple[ 0 ];
String className = (String) tuple[ 1 ];
}
Query q = session.createQuery
(" select members, classInfo.className " +
" from Members members, ClassInfo classInfo " +
" where members.level = classInfo.classCode ");
List result = q.list(); Iterator it = result.iterator();
while (it.hasNext()) { Object[] tuple = (Object[]) it.next();
Members members = (Members) tuple[ 0 ];
String className = (String) tuple[ 1 ]; }
这是获取Hibernate多表查询的结果的最常用的方式。
2、构造自己的复合类型,如下:
Java代码:
Query q = session.createQuery
(" select new NewMembers(members, classInfo.className) " +
" from Members members, ClassInfo classInfo " +
" where members.level = classInfo.classCode ");
Query q = session.createQuery
(" select new NewMembers(members, classInfo.className)
" + " from M
本文出自 “Mr_Computer” 博客,请务必保留此出处http://caochun.blog.51cto.com/4497308/1529865
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。