问题:
     我有张test表, 里面有发布时间和价格     然后我要根据功能来拼凑hql语句  用hibernate查询映射类      但是问题来了,如果我要是俩个都有排序 
      发布时间和价格 一起排序 
       order by 发布时间  desc  价格 asc
问题就是出在这个   逗号   “
     
 ------
手写代码  大小写以及中文符合请忽略。。
    stringbuffer hql = new stringbuffer();
     hql.append(“from  Test  as t where 1 = 1”)
     if(发布时间不为空){
           hql.append(“order by 发布时间 desc”);1   
      if(价格不为空){
           hql.append(“order by 价格 asc”);2           
}
}     我这个逗号不知道放在哪里   要放在1处  我有发布时间没价格 肯定会出错在逗号上
     同理 价格一样 各位是如何解决的。。
     

解决方案 »

  1.   

    要是两个都不为空,那HQL语句 order by 发布时间 desc,order by 价格 asc..这么写肯定不对呀~
      

  2.   

    to :LeeHomWong是啊,这个想不出来啊   如何order by   做好判断
    懵了
      

  3.   

    要是用map集合搞如何?难道是这样?
        if(发布时间不为空且价格不为空){
            order by 发布时间 desc,价格 asc
    }
        if(发布时间为空且价格不为空){
            order by 价格
    }         if(发布时间不为空切价格为空){
            order by 发布时间
    }
         0。0#
      

  4.   

    难道
      if  (发布时间为空且价格不为空){
           价格排序
    }
      if   (发布时间不为空且价格不为空){
           发布时间和价格一起排序
    }
      if(发布时间不为空且价格为空){
          发布时间排序
    }
        0.0#
      

  5.   

    stringbuffer hql = new stringbuffer();
      hql.append(“from Test as t where 1 = 1”)
      if(发布时间不为空 || 价格不为空) {
        hql.append(" order by ")
      }
     if(发布时间不为空){
      hql.append(“发布时间 desc ,”);1  
      if(价格不为空){
      hql.append(“ 价格 asc ,”);2  
    }}
    最后subString() 把最后的一个逗号去掉
      

  6.   

      stringbuffer hql = new stringbuffer();
      hql.append(“from Test as t where 1 = 1”)
      if(发布时间不为空)
      {
       hql.append(“order by 发布时间 desc”);1   
      }
      if(发布时间不为空 并且 价格不为空)
      {
        hql.append(“, order by 价格 asc”);2   
      }else if(发布时间为空 并且 价格不为空)
      {
        hql.append(“order by 价格 asc”);2 
      }