hibernate 分页排序问题
![]()
hibernate 分页排序问题
![]() 原因分析: 主要是由于hibernate底层机制导致的问题。当处于分页查询时候,处理第一页的sql(hql解析出)与处理第二页的sql不一样。第二页明显多包了一层。这样导致查询数据上的误差 例如: java代码如下: StringBuffer sql = new StringBuffer(" select s from KwmisApplyPscl as s where 1=1 "); StringBuffer sqlCount = new StringBuffer("select count(*) ,sum(s.kwmisApplyXmBudget.budget) , sum(s.kwmisApplyXmBudget.applyBudget),sum(s.kwmisApplyProject.prjBudget) from KwmisApplyPscl as s where 1=1 " ); // 默认查询待提交的项目 if("dtj".equals(theForm.getPageType())||StringUtils.isBlank(theForm.getPageType())){ // 待提交 sql.append(" and s.flowstatus in( "+ StaticVariable.XMYS_PSCL_STATUS4+","+StaticVariable.XMYS_PSCL_STATUS5+") "); sqlCount.append(" and s.flowstatus in( "+ StaticVariable.XMYS_PSCL_STATUS4+","+StaticVariable.XMYS_PSCL_STATUS5+") "); }else if("ytj".equals(theForm.getPageType())||StringUtils.isBlank(theForm.getPageType())) { // 已提交 sql.append(" and s.flowstatus in( "+ StaticVariable.XMYS_PSCL_STATUS6+","+StaticVariable.XMYS_PSCL_STATUS9+") "); sqlCount.append(" and s.flowstatus in( "+ StaticVariable.XMYS_PSCL_STATUS6+","+StaticVariable.XMYS_PSCL_STATUS9+") "); } // 项目名称 if(StringUtils.isNotBlank(theForm.getPrjName())) { sql.append(" and s.kwmisApplyProject.prjName like :prjName "); sqlCount.append(" and s.kwmisApplyProject.prjName like :prjName "); } //主管科室 if (StringUtils.isNotBlank(theForm.getDepartment())) { sql.append(" and s.kwmisApplyProject.department=:department"); sqlCount.append(" and s.kwmisApplyProject.department=:department"); } //主管工程师 if (StringUtils.isNotBlank(theForm.getEngineer())) { sql.append(" and s.kwmisApplyProject.prjEngineer=:prjEngineer"); sqlCount.append(" and s.kwmisApplyProject.prjEngineer=:prjEngineer"); } // 项目类别 String attributeType =theForm.getProjAttributeType(); if(attributeType!=null&&!"".equals(attributeType)){ String projAttribute=attributeType.substring(0, 1); String projType=attributeType.substring(1, 2); if(projAttribute!=null&&!"".equals(projAttribute)){ sql.append(" and s.kwmisApplyProject.prjAttribute=:prjAttribute"); sqlCount.append(" and s.kwmisApplyProject.prjAttribute=:prjAttribute"); } if(projType!=null&&!"".equals(projType)){ sql.append(" and s.kwmisApplyProject.prjType=:prjType"); sqlCount.append(" and s.kwmisApplyProject.prjType=:prjType"); } } // 所属计划 if(StringUtils.isNotBlank(theForm.getTopic())) { sql.append(" and s.kwmisApplyProject.topic =:topic") ; sqlCount.append(" and s.kwmisApplyProject.topic =:topic") ; } // 申报年度 if(StringUtils.isNotBlank(theForm.getBudgetYear())) { sql.append(" and s.kwmisApplyXmBudget.budgetYear=:budgetYear"); sqlCount.append(" and s.kwmisApplyXmBudget.budgetYear=:budgetYear") ; } Query queryCount = getSession().createQuery(sqlCount.toString()); Query query = getSession().createQuery(sql.toString()); // 项目名称 if(StringUtils.isNotBlank(theForm.getPrjName())) { query.setString("prjName", "%"+theForm.getPrjName()+"%"); queryCount.setString("prjName", "%"+theForm.getPrjName()+"%"); } //主管科室 if (StringUtils.isNotBlank(theForm.getDepartment())) { query.setString("department", theForm.getDepartment()); queryCount.setString("department", theForm.getDepartment()); } //主管工程师 if (StringUtils.isNotBlank(theForm.getEngineer())) { query.setString("prjEngineer",theForm.getEngineer()); queryCount.setString("prjEngineer", theForm.getEngineer()); } // 项目类别 if(attributeType!=null&&!"".equals(attributeType)){ String projAttribute=attributeType.substring(0, 1); String projType=attributeType.substring(1, 2); if(projAttribute!=null&&!"".equals(projAttribute)){ query.setString("prjAttribute", projAttribute); queryCount.setString("prjAttribute",projAttribute); } if(projType!=null&&!"".equals(projType)){ query.setString("prjType", projType); queryCount.setString("prjType",projType); } } // 所属计划 if(StringUtils.isNotBlank(theForm.getTopic())){ query.setString("topic", theForm.getTopic()); queryCount.setString("topic",theForm.getTopic()); } // 申报年度 if(StringUtils.isNotBlank(theForm.getBudgetYear())){ query.setString("budgetYear", theForm.getBudgetYear()); queryCount.setString("budgetYear",theForm.getBudgetYear()); } query.setFirstResult((theForm.getPageNO()-1)*theForm.getPageSize()); query.setMaxResults(theForm.getPageSize()); List list = query.list(); 解析第一页时候: select * from ( kwmisapply0_.FLOWSTATUS as FLOWSTATUS115_, kwmisapply0_.CREATE_DATE as CREATE3_115_, kwmisapply0_.APPLY_PRJ_ID as APPLY4_115_, kwmisapply0_.PRJ_APPLY_BUDGET_ID as PRJ5_115_ from KWMIS_APPLY_PSCL kwmisapply0_, KWMIS_APPLY_PROJECT kwmisapply1_, KWMIS_APPLY_XM_BUDGET kwmisapply2_ where kwmisapply0_.PRJ_APPLY_BUDGET_ID = kwmisapply2_.OID and kwmisapply0_.APPLY_PRJ_ID = kwmisapply1_.APPLY_PRJ_ID and 1 = 1 and (kwmisapply0_.FLOWSTATUS not in (1, 3)) and kwmisapply1_.DEPARTMENT =04 and kwmisapply2_.BUDGET_YEAR =2009 order by kwmisapply2_.REPLY_DATE desc where rownum <= 10 解析第二页时候: select * from (select row_.*, rownum rownum_ from (select kwmisapply0_.OID as OID, kwmisapply0_.FLOWSTATUS as FLOWSTATUS115_, kwmisapply0_.CREATE_DATE as CREATE3_115_, kwmisapply0_.APPLY_PRJ_ID as APPLY4_115_, kwmisapply0_.PRJ_APPLY_BUDGET_ID as PRJ5_115_ from KWMIS_APPLY_PSCL kwmisapply0_, KWMIS_APPLY_PROJECT kwmisapply1_, KWMIS_APPLY_XM_BUDGET kwmisapply2_ where kwmisapply0_.PRJ_APPLY_BUDGET_ID = kwmisapply2_.OID and kwmisapply0_.APPLY_PRJ_ID = kwmisapply1_.APPLY_PRJ_ID and 1 = 1 and (kwmisapply0_.FLOWSTATUS not in (1, 3)) and kwmisapply1_.DEPARTMENT = 04 and kwmisapply2_.BUDGET_YEAR = 2009 order by kwmisapply2_.REPLY_DATE desc) row_) where rownum_ <= 20 and rownum_ > 10 注意上面红不同颜色标注处 结果:就会出现第一页查询出来的数据在第二页中显示了而第二页应该显示的数据没有显示出来 结论:对于用hibernate分页处理数据时候要注意排序问题 |