Answer: Create a class named HibernateArrayLongType which extends UserType. Define the two core methods in this class as follows:
public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
throws HibernateException, SQLException {
Array value = rs.getArray(names[0]);
if (value != null) {
return value.getArray();
}
else
return null;
}
public void nullSafeSet(PreparedStatement statement, Object value, int index)
throws HibernateException, SQLException {
if (value == null) {
statement.setNull(index, Types.ARRAY);
} else {
List list = (List)value;
Long[] array = new Long[list.size()];
for (int i = 0; i < list.size(); i++) {
array[i] = list.get(i);
}
// the "integer" type name may be specific to Postgres
Array sqlArray = statement.getConnection().createArrayOf("integer", array);
statement.setArray(index, sqlArray);
}
}
In a class which extends HibernateDaoSupport, add this code:
List
Query query = getSession().getNamedQuery("queryName");
query.setParameter("ids", ids, Hibernate.custom(HibernateArrayLongType.class));
List
1 comment:
Hibernate.custom() - there is no such a method in Hibernate class.
Post a Comment