先看以下代码
List receiveList = offDao.findByHql(hql, page, row);
if(receiveList.size()>0&&receiveList!=null){
Iterator it = receiveList.iterator();
while(it.hasNext()){
Object[] object = (Object[])it.next();
OffAnnouncementReceive offReceive = new OffAnnouncementReceive();
String consigneeid=object[0].toString();
String flgs = object[1].toString();
String sendmail = object[2].toString();
String sendPeo = object[4].toString();
Date sendDate = (Date)object[5];
String announcementReceiveId = object[6].toString();
offReceive.setAnnouncementReceiveId(announcementReceiveId);
offReceive.setConsigneeId(consigneeid);
offReceive.setSendmailId(sendmail);
if(sendDate!=null&&!sendDate.equals("")){
offReceive.setSendDate(sdf.format(sendDate));
}
}
}
使用sql返回的list<Object>转成需要的实体对象,看下面的Map的使用,很方便(需要将Query对象setResultTransformer):
List list = session.createSQLQuery(sql)
.setResultTransformer(
Transformers.ALIAS_TO_ENTITY_MAP)
.list();
//此时,每个Object可以转换成一个Map
Map map = (Map) list.get(i);
map.get("student_id"); //这里的key 一定是 数据库对应的字段名才行