In the below query on passing a list of office codes, I get a list of countryID’s. Now instead of that, I need a Map In which the key will be countryID and its value as the list of office codes. Can you please help me.
Example: If we have offices as abc, def belong to country 123 and xyz belongs to 789, I need a Map like
(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;
}
}
Execute the following query:
This query will return a
List<Object[]>, each object array containing a countryID as first element and an office code as second element.Then iterate through the list, and populate your map.
Note: using a StringBuffer to concatenate String literals is counter-productive, and less readable. You’d better simply do: