Hibernate 使用HQL的 in 时要注意判断in的值(list)是否包含数据

如果你使用 HQL的 in,例如:

sessionFactory.getCurrentSession()
                    .createQuery("select hlInfo.id, count(id) from HouseImg where hlInfo.id in :idList")

这时候要确认 idList的size不为0

改为如下:

  if(idList.size()!=0) {
            List result = sessionFactory.getCurrentSession()
                    .createQuery("select hlInfo.id, count(id) from HouseImg where hlInfo.id in :idList")
                    .setParameterList("idList",idList)
                    .list();
  }

如果为空,你查询的时候会出错,因为语句变成了

select hlInfo.id, count(id) from HouseImg where hlInfo.id in ()
原文地址:https://www.cnblogs.com/wuyou/p/3445704.html