首页 > 代码库 > Hibernate-多表关联查询结果的处理方法
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