使用hibernate构造hql语句时order by的妙用

    /**
     * 终端更换记录 查询字段gsn相同的记录
     */
    public String queryStaposchange()throws Exception{
        p("start...");
        String postationid = request.getParameter("postationid");
        p(postationid);
        p("end...");
        //查询字段gsn相同的记录
//        List<Staposchange> staposchangeList =

staposchangeManager.queryStaposchange(postationid);
        
//        List<Staposchange> staposchangeList0 =

staposchangeManager.queryStaposchange(postationid,0);
//        List<Staposchange> staposchangeList1 =

staposchangeManager.queryStaposchange(postationid,1);
        List<Staposchange> staposchangeList;
        List<String> gsn = staposchangeManager.queryStaposchangegsn

(postationid);//获取gsn list
        List<Staposchangeimei> spcimeiList = new

ArrayList<Staposchangeimei>();
//        String gsn = "";
        p("mark1");
        if(gsn!=null){
            spcimeiList = new ArrayList<Staposchangeimei>();
            p("mark2");
        for(String str:gsn){
            
            staposchangeList =

staposchangeManager.queryStaposchangeByGsn(str);
            
            
            //一个站点可能有几个更换记录 把需要的更换记录封装到一个List

中去(List中数据为实体,即List<Entity>)
            //pos.formerimei(状态为0时)  pos.postationid  pos.imei(状态

为1时)  sta_pos_change.createtime
//            List<Staposchangeimei> spcimeiList = new

ArrayList<Staposchangeimei>();
            Staposchangeimei spcimei = new Staposchangeimei();
            for(Staposchange spc:staposchangeList){
                p("***************"+spc.getStatus());
                p("***************"+spc.getPos().getImei());
                p("***************"+spc.getCreatetime());
                if(spc.getStatus()==0){
                    String formerimei = spc.getPos().getImei();
                    spcimei.setFormerimei(formerimei);
                    spcimei.setPostationid(postationid);
//                    Date createtime = spc.getCreatetime();
//                    spcimei.setCreatetime(createtime);
                    p("mark3");
                }if(spc.getStatus()==1){
                    String imei = spc.getPos().getImei();
                    spcimei.setImei(imei);
                    spcimei.setPostationid(postationid);
                    Date createtime = spc.getCreatetime();//状态

为1时才是绑定时间
                    spcimei.setCreatetime(createtime);
                    p("mark4");
                }
            }
            spcimeiList.add(spcimei);
            p("mark5");
        }
        }
//        p(staposchangeList.get(0).getCreatetime());
//        p(staposchangeList.get(0).getGsn());
//        p(staposchangeList.get(0).getStatus());
//        p(staposchangeList.get(0).getPos().getImei());
//        p(staposchangeList.get(0).getStation().getStid());
//        p(staposchangeList.get(0).getCreatetime());
//        p(staposchangeList.get(0).getCreatetime());
//        p(staposchangeList.get(0).getCreatetime());
//        p(staposchangeList.get(0).getCreatetime());
//        p(staposchangeList.get(0).getCreatetime());
        
        
//        request.setAttribute("staposchangeList", staposchangeList);
        p("mark6");
        if(spcimeiList.size()==0){
//            request.setAttribute("staposchangeList", null);
            spcimeiList = null;
        }
        request.setAttribute("staposchangeList", spcimeiList);
        return "pos-alter";
    }

上面为XXXAction.java中的方法


 下面为XXXManager.java中的方法
   /**
     * 查询绑定的终端 查询字段gsn相同的记录
     * @param postationid
     * @author ylchou
     */
    @SuppressWarnings("unchecked")
    public List<Staposchange> queryStaposchangeByGsn(String gsn) throws

Exception{
//        String hql = " from Staposchange where station.stid=(select stid from Station where postationid=?)";
//        String hql = " from Staposchange where gsn in(select gsn from

Staposchange where station.stid=(select stid from Station where postationid=?) group by gsn)";
        String hql = " from Staposchange where gsn=?";//默认从小到大排列
        p("测试开始2...");
        List<Staposchange> list = this.entityDao.find(hql, gsn);
        p("测试结束2...");
        return list.size()==0?null:list;
    }



解决办法:在sql语句中根据时间从小到大排序

String hql = " from Staposchange where gsn=? order by createtime ";//默认从小到大排序


你可能感兴趣的:(Hibernate,Date,exception,String,list,null)