Hibernate 施用 HQL 查询 使用List 作为查询条件的注意
Hibernate 使用 HQL 查询 使用List 作为查询条件的注意
今天是需要过滤查询保单号,使用了 List 作为过滤条件,如下:
HQL: where id.publicPolicyId in(?)
java: query.setParameter(0, (ArrayList类型)tmpList);
但是总报错误,
Can not bind to Type java.util.ArrayList
问同事,原来更改条件设置方法:
query.setParameterList("plist", tmpList);
查询成功。
代码如下:
String hql = "select publicPolicyId from TMuster where musterDate >= ? and publicPolicyId between ? and ? "; Query query = session.createQuery(hql); query.setParameter(0, DateUtil.toDate("2010-01-01")); query.setParameter(1, policyIdStart); query.setParameter(2, policyIdEnd); List tmpList = query.list(); if(tmpList == null){return null;} // 过滤查询 TPolicyInsurantTemporary 非空条件 hql = "select id.publicPolicyId from TPolicyInsurantTemporary where id.publicPolicyId in(:plist) and name <> '' and cert <> '' and cerc <> '' "; query = session.createQuery(hql); query.setParameterList("plist", tmpList); tmpList = query.list(); if(tmpList == null){return null;} // 过滤查询 PublicPolicyTemporary 非空条件 hql = "from TPublicPolicyTemporary where publicPolicyId in(:plist) and applyName <> '' and applyCert <> '' and applyCerc <> '' and billNo <> '' "; query = session.createQuery(hql); query.setParameterList("plist", tmpList); tmpList = query.list(); if(tmpList == null){return null;} rstList = this.TPublicPolicyTrVO(tmpList);