Java Hibernate Projections.sqlGroupProjection aliasing

最后都变了- 提交于 2019-12-25 03:08:33

问题


I have a Criteria using Hibernate like this

public class Student
{
    private Integer id,discriminatorColumn,discriminatorValue;
    //other stuff omitted for brevity
} 

This entity has a discriminatorColumn which have values from 1 to 10 (never NULL) and each discriminatorColumn has a discriminatorValue which have a value from 1 to 100 (never NULL)

Example: (Student data)

ID DiscriminatorColumn DiscriminatorValue
1            1                  3
2            2                  4
3            1                  13

And so.

I am trying to get a SUM of the DiscriminatorValue grouping by DiscriminatorColumn

I mean, in the previous example if I filter by DiscriminatorColumn = 1 I would receive SUM=16.

final Criteria criteria = session.createCriteria(Student.class)
.setProjection(Projections.sqlGroupProjection(
    "sum({alias}.discriminatorValue) as rowCount",
    "hibernateAlias.discriminatorColumn having rowCount>0",
    new String[]{"rowCount"},
    Helper.HIBERNATE_INTEGER_INSTANCE
 ));

This creates the rightSQL I see something like this

select sum(discriminatorValue) as rowCount
from student
group by discriminator_column
having rowCount>0

The problem i have is when i set the resultSetTransformer to Student.class I could not find the way Hibernate populates the sum of discriminatorValue column result in discriminatorValue Java field.

I think that for that purpose is use the following parameter new String[]{"rowCount"}

But I have tried

new String[]{"discriminatorValue"}
new String[]{"this.discriminatorValue"}
new String[]{"{alias}.discriminatorValue"}
new String[]{"{studentAlias}.discriminatorValue"} using alias in Criteria
new String[]{"rowCount as discriminatorValue"} 
new String[]{"rowCount as this_.discriminatorValue"}
and So On..

But seems impossible what i am doing wrong? It's not possible? Sum the values as set it as a simple Property as retrieving regular from database.

I have written my own transformer for this but it's kind of annoying.


回答1:


I have found the answer by myself and i will post here to be able to help somebody.

Projections.sqlGroupProjection("sum({alias}.discriminatorValue) as rowCount","hibernateAlias.discriminatorColumn having rowCount>0",new String[]{"rowCount"},Helper.HIBERNATE_INTEGER_INSTANCE)

Seems the rowCount aliasing in new String[]{"rowCount"} is just for mysql syntax or is just ignore by Criteria engine is you need to use a alias to populate a property of your EntityClass use outer alias instead.

Example:

yourProjections.add(Projections.alias(Projections.sqlGroupProjection("sum({alias}.discriminatorValue) as rowCount","hibernateAlias.discriminatorColumn having rowCount>0",new String[]{"rowCount"},Helper.HIBERNATE_INTEGER_INSTANCE),"discriminatorValue"));

Just wrap your sqlGroupProjections in a Projection.alias as set the alias match your property in your Java class and Hibernate will populate with the sum result and not custom transformer are not longer required..

I hope helps somebody.



来源:https://stackoverflow.com/questions/30215528/java-hibernate-projections-sqlgroupprojection-aliasing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!