select new com.telezone.hqllist.CardinfoList( cinfo.cardid ,cinfo.cardname , oinfo.ownername ,uinfo.username , cinfo.phonenum,cinfo.cardstate,cinfo.regdate,cinfo.otherinfo ) from Cardinfo as cinfo left join cinfo.cardownerinfo as oinfo left join cinfo.userinfo as uinfo
查询出来的是多个对象的属性,这样时候一个POJO是搞不定的,
所以创建了CardinfoList,里面的属性就是查询所有返回的属性
这样返回的List正好在Struts中可用
请问下,大家碰到这样的返回多个对象的属性时候是如何处理的?
让它直接返回Object[] 还是封装成一个对象,或者是其他办法?
5 个解决方案
#1
创建构造方法,有按你需要的去创建!
#2
创建构造方法,有按你需要的去创建!
#3
构造方法!
#4
用 List 或 Object[] 都可以处理的
#5
处理多表查询的结构通常有两种方法:
1、 对各个字段分别转化成对应类型,如下:
Java code
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 ]; }
这时,List的每一属性都是一个对象,通过 Members members = (Members) tuple[ 0 ]转换类型。
2、构造自己的复合类型,如下:
Java code
Query q = session.createQuery(" select new NewMembers(members, classInfo.className) " +
" from Members members, ClassInfo classInfo " +
" where members.level = classInfo.classCode ");
这是需要相应的NewMembers类和构造方法。
1、 对各个字段分别转化成对应类型,如下:
Java code
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 ]; }
这时,List的每一属性都是一个对象,通过 Members members = (Members) tuple[ 0 ]转换类型。
2、构造自己的复合类型,如下:
Java code
Query q = session.createQuery(" select new NewMembers(members, classInfo.className) " +
" from Members members, ClassInfo classInfo " +
" where members.level = classInfo.classCode ");
这是需要相应的NewMembers类和构造方法。
#1
创建构造方法,有按你需要的去创建!
#2
创建构造方法,有按你需要的去创建!
#3
构造方法!
#4
用 List 或 Object[] 都可以处理的
#5
处理多表查询的结构通常有两种方法:
1、 对各个字段分别转化成对应类型,如下:
Java code
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 ]; }
这时,List的每一属性都是一个对象,通过 Members members = (Members) tuple[ 0 ]转换类型。
2、构造自己的复合类型,如下:
Java code
Query q = session.createQuery(" select new NewMembers(members, classInfo.className) " +
" from Members members, ClassInfo classInfo " +
" where members.level = classInfo.classCode ");
这是需要相应的NewMembers类和构造方法。
1、 对各个字段分别转化成对应类型,如下:
Java code
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 ]; }
这时,List的每一属性都是一个对象,通过 Members members = (Members) tuple[ 0 ]转换类型。
2、构造自己的复合类型,如下:
Java code
Query q = session.createQuery(" select new NewMembers(members, classInfo.className) " +
" from Members members, ClassInfo classInfo " +
" where members.level = classInfo.classCode ");
这是需要相应的NewMembers类和构造方法。