public List findAll(Long userid, Long cardid) {
Session session = HibernateSessionFactory.getSession();
StringBuffer sb = new StringBuffer();
Transaction tx = null;
Query queryObject = null;
try {
tx = session.beginTransaction();
sb
.append("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 where uinfo.userid = :uid ");
// 定位卡
if (null != cardid && !"".equals(cardid)) {
sb.append(" and cinfo.cardid = :cid");
}
queryObject = session.createQuery(sb.toString());
queryObject.setLong("uid", userid);
queryObject.setParameter("cid", cardid);
tx.commit(); } catch (RuntimeException re) {
tx.rollback();
log.error("Cardinfo finaAll failed", re);
}
return queryObject.list();
}
在aciton里面调用这个方法,传值后list。sieze为0
但是如果写成这样,where uinfo.userid = 200就有数据,这是为什么?还有一个问题:当我传值20,null时,程序报错
org.hibernate.QueryParameterException: could not locate named parameter [cid]请问如何解决?
对于你的第一个问题,我感觉不是程序的问题,原因可能是你数据库中的数据userid=200时才有数据,当userid=20时没有数据,你再看看数据库是不是没有你想查的用户id
pojo类跟映射文件中的类型是不是都是Long,还有在数据库中是不是bigint