I have a bean FooEntity object and a DAO method to get the count of rows grouped by a property on this object enclosed in a bean FooCount view model.
public List<FooCount> groupByFoo() { return sessionFactory.getCurrentSession() .createCriteria(FooEntity.class) .setProjection(Projections.projectionList() .add(Projections.groupProperty("foo"), "foo") .add(Projections.count("foo"), "count") ).setResultTransformer(Transformers.aliasToBean(FooCount.class)) .list(); } public class FooCount { private String foo; private Integer count;
Doing this throws an exception, since Projections.count() gives Long instead of Integer .
org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of FooCount.count at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:119) --snip-- Caused by: java.lang.IllegalArgumentException: argument type mismatch
It works if I change count to Long , but I would prefer not to change the class of the view model, as it is used elsewhere.
Is it possible to either make Projections.count() return a Integer some way, or to convert the result transformer from Long to Integer ?
Zutty source share