2017-10-10 125 views
1

我想運行下面的代碼,但我得到一個錯誤。像在HQL運營商%

public MdFeedHandOff getFeedByHandOff(Integer handoff, Integer csiID){ 
     logger.error("*************getFeedByHandOff(): handoff value is "+handoff); 
     String hql = "FROM MdFeedHandOff a WHERE a.handoff = :handoff and a.active in ('A','I') and to_char(a.handoff) like :csiID%"; 
     Map<String, Object> param = new HashMap<String, Object>(); 
     param.put("handoff", handoff); 
     List<MdFeedHandOff> batchJobList = searchForList(hql, param); 
     return batchJobList.isEmpty()?null:batchJobList.get(0); 
    } 

我想在hql中使用像%運算符。請幫助我。 我也在線下嘗試,但它不工作。

String hql = "FROM MdFeedHandOff a WHERE a.handoff = :handoff and a.active in ('A','I') and to_char(a.handoff) like :csiID" + "%"; 
+0

什麼裏面searchForList?如何創建查詢並設置參數? –

+0

@SuppressWarnings( 「未登記」) \t公開名單searchForList(最後字符串的句子,最後地圖<字符串,對象>參數){ \t \t回報(名單)getHibernateTemplate()。執行(新HibernateCallback(){ \t \t \t公共對象doInHibernate(會話會話)拋出HibernateException的{ \t \t \t \t查詢的查詢= session.createQuery(句子); \t \t \t \t query.setProperties(參數); \t \t \t \t return query.list(); \t \t \t} \t \t}); \t} –

回答

2

你應該只把:csid參數名稱查詢和您設置的參數添加%通配符。你也似乎沒有在params地圖中設置csid。

查詢

FROM MdFeedHandOff a WHERE a.handoff = :handoff 
    and a.active in ('A','I') and to_char(a.handoff) like :csiID"; 

PARAMS

Map<String, Object> param = new HashMap<String, Object>(); 
param.put("handoff", handoff); 
param.put("csiID", csiId.toString()+"%");