我有一个实体 beanFooEntity
和 DAO 方法来获取按该实体上的属性分组的行计数,封装在视图模型 beanFooCount
中。
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; // <-- this is the problem
// getters/setters...
}
运行它会产生异常,因为会Projections.count()
产生 aLong
而不是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
如果我更改count
为 a ,它Long
会起作用,但我不想更改视图模型类,因为它用于其他各种地方。
我可以以某种方式Projections.count()
返回或使结果转换器转换为吗?Integer
Long
Integer