Hibernate的Hql查询无法设置最大查询数。把 query.setMaxResults(1);去掉程序就正确。加上就错误
publicList<SurveyInfo>findSurveyInfoByCondition(intpage,StringSurveyno,StringplateNo)...
public List<SurveyInfo> findSurveyInfoByCondition(int page, String Surveyno,
String plateNo) throws DAOException, DataNotFoundException {
List list = new ArrayList();
try {
Session session = HibernateUtil.getSession();
String hql = "from SurveyInfo s left join fetch s.report r left join fetch r.policy p left join fetch p.vehicle v where 1=1";
if (Surveyno != null) {
hql += " and s.surveyNo like '%" + Surveyno + "%'";
}
if (plateNo != null) {
hql += " and v.plateno like '%" + plateNo + "%'";
}
hql+=" order by s.id asc";
Query query = session.createQuery(hql);
query.setFirstResult(1 + (15*(1 - 1)));
// query.setMaxResults(1);
list = query.list();
if (list.size() == 0) {
throw new DataNotFoundException("找不到相关数据");
}
} catch (HibernateException e) {
throw new DAOException(e.getMessage());
} finally {
HibernateUtil.close();
}
return list;
} 展开
String plateNo) throws DAOException, DataNotFoundException {
List list = new ArrayList();
try {
Session session = HibernateUtil.getSession();
String hql = "from SurveyInfo s left join fetch s.report r left join fetch r.policy p left join fetch p.vehicle v where 1=1";
if (Surveyno != null) {
hql += " and s.surveyNo like '%" + Surveyno + "%'";
}
if (plateNo != null) {
hql += " and v.plateno like '%" + plateNo + "%'";
}
hql+=" order by s.id asc";
Query query = session.createQuery(hql);
query.setFirstResult(1 + (15*(1 - 1)));
// query.setMaxResults(1);
list = query.list();
if (list.size() == 0) {
throw new DataNotFoundException("找不到相关数据");
}
} catch (HibernateException e) {
throw new DAOException(e.getMessage());
} finally {
HibernateUtil.close();
}
return list;
} 展开
2个回答
展开全部
改变查询sql进行测试,先排除sql问题再看看
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
hql语句本身有大问题。左连接那么多的对象,from前面却没有select 关键字,hibernate怎么知道该给你哪个对象,hibernate就一定会把结果关联到你要的实体类SurveyInfo上吗?看你方法返回的是List<SurveyInfo>集合,hibernate会这么智能给你List<SurveyInfo>吗。首先你的hibernate查询结果的知识都没搞懂,分页就放到一边去讨论吧。像你那样写hql语句,还不如写Sql语句,让hibernate调用。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询