2013-08-20 84 views
0

問題:hql查詢的IN子句只匹配逗號分隔列表的第一項!Hibernate HQL IN子句只匹配列表中的第一項

HQL查詢是這樣的:from News where 93 IN (pictureIds)

新聞單位是:

public class News { 
    String id; 
    String pictureIds; // comma separated list of pictureIds 
} 

注意:如果我改變HQL查詢和硬編碼pictureIds其結果是正確的!

如何更改我的查詢來解決問題?

+0

你使用Hibernate模板,這樣做有助於將所有的變化逗號前..? – Dileep

+0

我正在使用Hibernate,Criteria和Criterion來構建HQL。 – SMHJamali

回答

1

試試這個:

from News where pictureIds LIKE '%,93,%' 

逗號添加到開始和結束。

+0

它不起作用,無所適從。 – SMHJamali

+0

已更新。希望這項工作 – user2698469

+0

最後我決定改變數據庫結構。 – SMHJamali

0

這樣做。

Query query = getSession().createQuery("Select * from News where pictureIds In (:List)") 
      .setParameterList("List",ListValues); 

here的HQL使用的基本知識。

0

我已完成了工作,但其相當繁瑣:

results?.eachWithIndex { output, i-> 
        if (i>0) { 
         sb.append(" or ") 
        } 
        sb.append(" ((dbres.Field like (:myVariablesA${i})) or (dbres.Field like (:myVariablesB${i})) or (dbres.Field like (:myVariablesC${i})) or dbres.Field=:myVariable${i})") 
        //capture within csv so aa,val,bb 
        myParams."myVariablesA${i}"='%,'+output+',%' 
        //capture as last element: aa,val 
        myParams."myVariablesB${i}"='%,'+output 
        //capture it as first element val,aa 
        myParams."myVariablesC${i}"=output+',%' 
        //finally capture it if its not a csv and its a physical set value 
        myParams."myVariable${i}"=output 
       } 

這是常規,並追加到一個StringBuilder(SB)元素。總之,csv字符串字段的%,id,%本身太強大了。通過後自行捕捉,可以在實際的字符串存在的所有變化

希望它能別人