private int getTotal(final int page, final int pageSize,final int productTypeId, final int progress,final String orderCode,final Date startDate, final Date stopDate,final String clientUserName) {
        Session session = (Session) this.hibernateTemplate.getSessionFactory().openSession();            StringBuffer buff = new StringBuffer();
            buff.append(" from Orders o ");
            boolean where = false;
            if(productTypeId !=0 && !"".equals(productTypeId)){
                buff.append(" where o.workBillInfo.productType.productTypeId=:productTypeId ");
                where = true;
            }
            if (progress!=0 && !"".equals(progress)) {
                if(where){
                    buff.append(" and ");
                }else{
                    buff.append(" where ");
                    where = true;
                }
                buff.append("  o.progress.progressCode=:progress ");
            }     
            if (progress==0) {
                if(where){
                    buff.append(" and ");
                }else{
                    buff.append(" where ");
                    where = true;
                }
                buff.append("  o.progress.progressCode!=:progressC ");
            }
            
            if (orderCode != null && !"".equals(orderCode)) {
                if(where){
                    buff.append(" and ");
                }else{
                    buff.append(" where ");
                    where =true;
                }
                buff.append(" o.orderCode=:orderCode ");
            }
            
            if (startDate != null && !"".equals(startDate)
                    && stopDate!= null && !"".equals(stopDate)) {
                if (where) {
                    buff.append(" and ");
                } else {
                    buff.append(" where ");
                    where = true;
                }
                buff.append(" o.orderTime between :startDate and :stopDate ");
            }
            if(where){
                buff.append(" and ");
            }else{
                buff.append(" where ");
                where =true;    
            }
                buff.append(" o.clientLoginInfo.clientUserName=:clientUserName ");    
                Query query=session.createQuery(buff.toString());   
             
                 if (progress!=0 && !"".equals(progress)) {
                    query.setInteger("progress", progress);
                } 
                 if (productTypeId != 0 && !"".equals(productTypeId)) {
                    query.setInteger("productTypeId", productTypeId);
                }
                 if (orderCode != null && !"".equals(orderCode)) {
                    query.setString("orderCode", orderCode);
                }
                 if (progress==0) {
                    query.setInteger("progressC", 17);
                } 
                if (startDate != null && stopDate != null) {
                    query.setDate("startDate", startDate);
                    query.setDate("stopDate", stopDate);
                }
                query.setString("clientUserName", clientUserName);
                return query.list().size();        
    }
我打了断点,只要一执行到return那句  控制台就打印出20多次相同的查询 求解

解决方案 »

  1.   

    我记得Hibernate就是这样,先查询符合条件的记录id,然后根据id一一去查询数据
      

  2.   

    建议  SQL 条件到业务逻辑层写然后作为参数传进来
      

  3.   

    格式太乱了 先帮你整理下再看
    private int getTotal(final int page, final int pageSize,final int productTypeId, final int progress,final String orderCode,final Date startDate, final Date stopDate,final String clientUserName) {
      Session session = (Session) this.hibernateTemplate.getSessionFactory().openSession();  StringBuffer buff = new StringBuffer();
      buff.append(" from Orders o ");
      boolean where = false;
      if(productTypeId !=0 && !"".equals(productTypeId)){
      buff.append(" where o.workBillInfo.productType.productTypeId=:productTypeId ");
      where = true;
      }
      if (progress!=0 && !"".equals(progress)) {
      if(where){
      buff.append(" and ");
      }else{
      buff.append(" where ");
      where = true;
      }
      buff.append(" o.progress.progressCode=:progress ");
      }   
      if (progress==0) {
      if(where){
      buff.append(" and ");
      }else{
      buff.append(" where ");
      where = true;
      }
      buff.append(" o.progress.progressCode!=:progressC ");
      }
        
      if (orderCode != null && !"".equals(orderCode)) {
      if(where){
      buff.append(" and ");
      }else{
      buff.append(" where ");
      where =true;
      }
      buff.append(" o.orderCode=:orderCode ");
      }
        
      if (startDate != null && !"".equals(startDate)
      && stopDate!= null && !"".equals(stopDate)) {
      if (where) {
      buff.append(" and ");
      } else {
      buff.append(" where ");
      where = true;
      }
      buff.append(" o.orderTime between :startDate and :stopDate ");
      }
      if(where){
      buff.append(" and ");
      }else{
      buff.append(" where ");
      where =true;   
      }
      buff.append(" o.clientLoginInfo.clientUserName=:clientUserName ");   
      Query query=session.createQuery(buff.toString());   
        
      if (progress!=0 && !"".equals(progress)) {
      query.setInteger("progress", progress);
      }  
      if (productTypeId != 0 && !"".equals(productTypeId)) {
      query.setInteger("productTypeId", productTypeId);
      }
      if (orderCode != null && !"".equals(orderCode)) {
      query.setString("orderCode", orderCode);
      }
      if (progress==0) {
      query.setInteger("progressC", 17);
      }  
      if (startDate != null && stopDate != null) {
      query.setDate("startDate", startDate);
      query.setDate("stopDate", stopDate);
      }
      query.setString("clientUserName", clientUserName);
      return query.list().size();   
      }
      

  4.   

    1.  sql语句没有!=一说 用<>
    2.  写这么多就为了判断第一个是and还是where 直接在buff.append(" from Orders o where 1=1 ");后面都是and就好了