Java 休眠 Projections.sqlGroupProjection 别名
Java Hibernate Projections.sqlGroupProjection aliasing
我有一个 Criteria
像这样使用 Hibernate
public class Student
{
private Integer id,discriminatorColumn,discriminatorValue;
//other stuff omitted for brevity
}
此实体有一个 discriminatorColumn
,其值从 1 到 10(从不 NULL
),每个 discriminatorColumn
都有一个 discriminatorValue
,其值从 1 to 100
(从不NULL
)
示例:(学生数据)
ID DiscriminatorColumn DiscriminatorValue
1 1 3
2 2 4
3 1 13
等等。
我正在尝试通过 DiscriminatorColumn
获得 DiscriminatorValue
分组的 SUM
我的意思是,在前面的示例中,如果我按 DiscriminatorColumn = 1
过滤,我将收到 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
));
这创建了 rightSQL
我看到了这样的东西
select sum(discriminatorValue) as rowCount
from student
group by discriminator_column
having rowCount>0
我遇到的问题是,当我将 resultSetTransformer
设置为 Student.class 时,我找不到 Hibernate 填充 discriminatorValue
列的 sum
的方式,导致 discriminatorValue
Java 字段.
我认为为此目的使用以下参数 new String[]{"rowCount"}
不过我试过了
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..
但似乎不可能我做错了什么?这是不可能的? Sum the values as set it as a simple Property as retrieving regular from database
.
我为此编写了自己的转换器,但有点烦人。
我找到答案了:
Projections.sqlGroupProjection("sum({alias}.discriminatorValue) as rowCount","hibernateAlias.discriminatorColumn having rowCount>0",new String[]{"rowCount"},Helper.HIBERNATE_INTEGER_INSTANCE)
似乎 new String[]{"rowCount"}
中的 rowCount
别名仅适用于 MySQL 语法或只是被 Criteria engine
忽略是否需要使用别名来填充 属性 的 EntityClass 使用 outer alias instead
.
示例:
yourProjections.add(Projections.alias(Projections.sqlGroupProjection("sum({alias}.discriminatorValue) as rowCount","hibernateAlias.discriminatorColumn having rowCount>0",new String[]{"rowCount"},Helper.HIBERNATE_INTEGER_INSTANCE),"discriminatorValue"));
只需将 sqlGroupProjections
包装在 Projection.alias
中,因为设置别名与 Java class 中的 属性 相匹配,Hibernate 将填充总和结果而不是 custom transformer are not longer required
.
我有一个 Criteria
像这样使用 Hibernate
public class Student
{
private Integer id,discriminatorColumn,discriminatorValue;
//other stuff omitted for brevity
}
此实体有一个 discriminatorColumn
,其值从 1 到 10(从不 NULL
),每个 discriminatorColumn
都有一个 discriminatorValue
,其值从 1 to 100
(从不NULL
)
示例:(学生数据)
ID DiscriminatorColumn DiscriminatorValue
1 1 3
2 2 4
3 1 13
等等。
我正在尝试通过 DiscriminatorColumn
DiscriminatorValue
分组的 SUM
我的意思是,在前面的示例中,如果我按 DiscriminatorColumn = 1
过滤,我将收到 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
));
这创建了 rightSQL
我看到了这样的东西
select sum(discriminatorValue) as rowCount
from student
group by discriminator_column
having rowCount>0
我遇到的问题是,当我将 resultSetTransformer
设置为 Student.class 时,我找不到 Hibernate 填充 discriminatorValue
列的 sum
的方式,导致 discriminatorValue
Java 字段.
我认为为此目的使用以下参数 new String[]{"rowCount"}
不过我试过了
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..
但似乎不可能我做错了什么?这是不可能的? Sum the values as set it as a simple Property as retrieving regular from database
.
我为此编写了自己的转换器,但有点烦人。
我找到答案了:
Projections.sqlGroupProjection("sum({alias}.discriminatorValue) as rowCount","hibernateAlias.discriminatorColumn having rowCount>0",new String[]{"rowCount"},Helper.HIBERNATE_INTEGER_INSTANCE)
似乎 new String[]{"rowCount"}
中的 rowCount
别名仅适用于 MySQL 语法或只是被 Criteria engine
忽略是否需要使用别名来填充 属性 的 EntityClass 使用 outer alias instead
.
示例:
yourProjections.add(Projections.alias(Projections.sqlGroupProjection("sum({alias}.discriminatorValue) as rowCount","hibernateAlias.discriminatorColumn having rowCount>0",new String[]{"rowCount"},Helper.HIBERNATE_INTEGER_INSTANCE),"discriminatorValue"));
只需将 sqlGroupProjections
包装在 Projection.alias
中,因为设置别名与 Java class 中的 属性 相匹配,Hibernate 将填充总和结果而不是 custom transformer are not longer required
.