我已经解决了使用HQL的问题,并且效果很好。

但我确实喜欢Criteria API。 (我的HQL的querybuilder字符串中有一些ifs语句,是的)
显然Projections.sum(property)返回double。

我的实体课有

@Column(name = "current_volume")
private Integer currentVolume;


我得到的错误是


org.hibernate.PropertyAccessException:调用se.unox.pejl.entity.value.pejl.PejlDataTrendValue.currentVolume的设置程序时发生IllegalArgumentException。


我的等效工作质量是

select cast(sum(p.currentVolume/1000) as integer) as currentVolume from
se.unox.pejl.entity.value.pejl.PejlDataTrendValue as p


我想我知道问题是什么,但无法弄清楚如何将列的总和(在mysql中为INT(11))转换为Integer。显然NHibernate有投影。

我正在使用Hibernate 3.6

最佳答案

基于this blog post(除非在3.5和3.6之间进行了更改),Projections.sum的返回类型取决于要求和的属性的属性类型:

对于映射为Long,Short,Integer或原始整数类型的属性,返回Long值;
对于映射为Float,Double或原始浮点类型的属性,将返回Double值。

如果要覆盖本机功能,请follow the answer in this post

09-11 17:24