[英]Return a Map from a hibernate query
在下面關於傳遞辦公代碼列表的查詢中,我獲得了countryID的列表。 現在代替它,我需要一個Map,其中的鍵將是countryID,其值將作為辦公代碼列表。 你能幫我么。
示例:如果我們有辦公室,例如abc,def屬於國家123 , xyz屬於789 ,我需要一個地圖,如(123,List(abc(def,))...(789,List(xyz)))
public List getData(List officeCode) {
try {
StringBuffer queryString = new StringBuffer("select distinct
(abc.countryID) from com.#####.TABLE table");
queryString.append(" where table.officeCode in (:oCode)");
return SessionFactory.getCurrentSession()
.createQuery(queryString.toString())
.setParameterList("oCode",officeCode )
.list();
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}
執行以下查詢:
select distinct abc.countryID, abc.officeCode from SomeEntity abc where abc.officeCode in (:codes)
該查詢將返回List<Object[]>
,每個對象數組都包含一個countryID作為第一個元素,並包含一個辦公代碼作為第二個元素。
然后遍歷列表,並填充地圖。
注意:使用StringBuffer連接String文字會適得其反,並且可讀性較低。 你最好干脆做:
String queryString = "select distinct (abc.countryID) from com.#####.TABLE table"
+ " where table.officeCode in (:oCode)";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.