2 yanghang888888 yanghang888888 于 2017.09.11 17:55 提问

hibernate 返回集合怎么转实体对象

我的hibernate后台查出一个集合, public List selectUserPwdByIsadmin() {
SessionFactory sessionFactory = this.getHibernateTemplate().getSessionFactory();
Session session = sessionFactory.openSession();
String hqlSelect = "select unit,pwd from aca_resource_subaccount where isadmin=1";
List list = session.createSQLQuery(hqlSelect).list();
return list;

}在action里面怎么转成AcaResourceSubaccount实体获取我想要的数据,我遍历用报错,说object没办法转成AcaResourceSubaccount

2个回答

swd419459401
swd419459401   2017.09.11 18:14

你把hql语句改成from aca_resource_subaccount where isadmin=1试试,然后再遍历集合

yanghang888888
yanghang888888   2017.09.11 18:23

public List selectUserPwdByIsadmin() {
SessionFactory sessionFactory = this.getHibernateTemplate().getSessionFactory();
Session session = sessionFactory.openSession();
String hqlSelect = "select unit,pwd from aca_resource_subaccount where isadmin=:isadmin";
SQLQuery createSQLQuery = session.createSQLQuery(hqlSelect);
createSQLQuery.setBigDecimal("isadmin", new BigDecimal("1"));
createSQLQuery.setResultTransformer(Transformers.aliasToBean(AcaResourceSubaccount.class));
List list=null;
list=createSQLQuery.list();
session.close();
return list;
}已经解决了,谢谢了 这样的话action里就能遍历了

wgs1393044648
wgs1393044648 回复无歆可行: 返回强转成list<类名>(这怎么打回车....)
2 个月之前 回复
wgs1393044648
wgs1393044648 hql="from 类名 where id =?"
2 个月之前 回复
wgs1393044648
wgs1393044648 为什么会这么麻烦?
2 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片