2016-09-07 55 views
0

我想實現DataTable的服務器端過濾。以下是檢索列表的服務方法:不能在where子句中使用表達式

@Override 
    @Transactional 
    public List<User> list(int start, int length, String search, int triIdx, String ordreTri) { 

     String hql = "from User "; 

     if (!search.equals("") && search != null) { 

      hql = hql.concat(" where "); 

      if (NumberUtils.isNumber(search)) 
       hql = hql.concat(" salary "); 
      else 
       hql = hql.concat(" lower(username) "); 

      hql = hql.concat(" like lower('%:critere%') "); 

     } 

     if (ordreTri.equals("asc")) { 
      switch (triIdx) { 
       case 0: 
        hql = hql.concat(" order by username "); 
        break; 
       case 1: 
        hql = hql.concat(" order by email "); 
        break; 
       case 2: 
        hql = hql.concat(" order by salary "); 
        break; 
       default: 
        hql = hql.concat(" order by username "); 
        break; 
      } 
     } else { 
      switch (triIdx) { 
      case 0: 
       hql = hql.concat(" order by username desc"); 
       break; 
      case 1: 
       hql = hql.concat(" order by email desc"); 
       break; 
      case 2: 
       hql = hql.concat(" order by salary desc"); 
       break; 
      default: 
       hql = hql.concat(" order by username desc"); 
       break; 
      } 
     } 

     Query query = sessionFactory.getCurrentSession().createQuery(hql); 
     query = query.setParameter("critere",search); 
     query = query.setFirstResult(start); 
     query = query.setMaxResults(length); 

     @SuppressWarnings("unchecked") 
     List<User> listUser = (List<User>) query.list(); 
     return listUser; 
    } 

在運行時,我得到一個關於數據的dataTable警報錯誤。那麼我的代碼有什麼問題?

+0

沒有性判據。您的字符串已被轉義,因此查詢按原樣執行並且不包含名爲'critere'的條件。在設置之前,刪除轉義字符並在'%'後綴前綴/後綴。 –

回答

0
hql.concat(" like lower('%:critere%') ") 

應rewrittent到:

hql.concat(" like :critere ") 
query = query.setParameter("critere", "%" + search.toLowerCase(Locale. ....) + "%"); 
相關問題