String Query = "Select name from Customer where city in (:city)";
Map<String, Object> values = new HashMap<String, Object>();
ArrayList<String> city = new ArrayList<String>();
city.add("Bangalore");
city.add("Hyderabad");
values.put("city" , city);
...
public List getResult(String query, Map<String, Object> values) {
Session session = getSession(true);
SQLQuery sqlQuery = session.createSQLQuery(query);
if(!values.isEmpty()) {
for (String key : values.keySet()) {
Object value = values.get(key);
if (value instanceof Collection) {
sqlQuery.setParameterList(key, (Collection) value);
} else {
sqlQuery.setParameter(key, value);
}
}
}
return sqlQuery.list();
}
使用org.hibernate.SQLQuery的setParameterList
,该列表应该在查询中加上逗号('Bangalore', 'Hyderabad')
。但它在查询中没有逗号,如('Bangalore''Hyderabad')
。那是错的。查询将无法正确执行。我该如何解决这个问题?