@Corey's solution works fine, but it includes a problem inside a for loop where query.setParameter (...) is called.
, , , , ClassCastExceptions, Hibernate , getId() ( ). , , IN- (,... WHERE department IN (: department)...), "department" - .
, 'query.setParameterList(paramName, (Object []) value)' 'query.setParameterList(paramName, (Collection) value)'
:
@Corey, applyNamedParameterToQuery(), org.springframework.orm.hibernate3.HibernateTemplate.applyNamedParameterToQuery(Query, String, Object):
import java.sql.SQLException;
import java.util.List;
import org.apache.poi.hssf.record.formula.functions.T;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
public class HibernateCallbackImpl
implements HibernateCallback<List<T>> {
private String queryString;
private String[] paramNames;
private Object[] values;
private int firstResult;
private int maxResults;
public HibernateCallbackImpl(
String queryString,
String[] paramNames,
Object[] values,
int firstResult,
int maxResults) {
this.queryString = queryString;
this.paramNames = paramNames;
this.values = values;
this.firstResult = firstResult;
this.maxResults = maxResults;
}
@Override
public List<T> doInHibernate(Session session) throws HibernateException,
SQLException {
Query query = session.createQuery(queryString);
query.setFirstResult(firstResult);
query.setMaxResults(maxResults);
for (int c=0; c<paramNames.length; c++) {
applyNamedParameterToQuery(query, paramNames[c], values[c]);
}
@SuppressWarnings("unchecked")
List<T> result = query.list();
return result;
}
protected void applyNamedParameterToQuery(Query queryObject, String paramName, Object value)
throws HibernateException {
if (value instanceof Collection) {
queryObject.setParameterList(paramName, (Collection) value);
}
else if (value instanceof Object[]) {
queryObject.setParameterList(paramName, (Object[]) value);
}
else {
queryObject.setParameter(paramName, value);
}
}
}