What is Hibernate equivalent of Projections.Cast?
I have fixed my problem using HQL and it is working great.
But I do like Criteria API. (I have a few ifs statements in my querybuilder string for HQL, yuck) Apparently Projections.sum(property) return double.
My Entity class has
@Column(name = "current_volume")
private Integer currentVolume;
The error I get is
org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of se.unox.pejl.entity.value.pejl.PejlDataTrendValue.currentVolume.
My equivalent working hql is
select cast(sum(p.currentVolume/1000) as integer) as currentVolume from
se.unox.pejl.开发者_如何学Goentity.value.pejl.PejlDataTrendValue as p
I think I know what the problem is, but cannot figure out how to cast sum of columns (which is INT(11) in mysql) into Integer. Apparently NHibernate has Projections.Cast
I am using Hibernate 3.6
Based on this blog post (unless something was changed between 3.5 and 3.6), the return type of Projections.sum
is dependent on the property type of the property being summed:
for properties mapped as Long, Short, Integer, or primitive integer types, a Long value is returned;
for properties mapped as Float, Double, or primitive floating point types, a Double value is returned.
If you want to override native functionality, follow the answer in this post.
精彩评论