我试图找出是否可以使用Hibernate Criteria API执行以下HQL:
String hql = "select new InitialCount(substring(name, 1,1), count(id)) from Person group by substring(name, 1,1)";
其中InitialCount是一个非常简单的bean,带有字符串和长构造函数:
public static class InitialCount {
private final String initial;
private final long count;
public InitialCount(String initial, long count) {
this.initial = initial;
this.count = count;
}
}
有可能吗?
答案 0 :(得分:4)
试试这个:
criteria.setProjection(Projections.projectionList()
.add(Projections.groupProperty("formulaProp").as("initial"))
.add(Projections.count("id").as("count"))
).setResultTransformer(Transformers.aliasToBean(InitialCount.class)).list();
其中formulaProp是使用公式“substring(name,1,1)”
映射的人的属性