我试图找出是否可以使用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;
    }
}


可能吗?

最佳答案

尝试这个 :

criteria.setProjection(Projections.projectionList()
        .add(Projections.groupProperty("formulaProp").as("initial"))
        .add(Projections.count("id").as("count"))
            ).setResultTransformer(Transformers.aliasToBean(InitialCount.class)).list();




其中,公式属性是映射到公式“ substring(name,1,1)”的人员的属性

09-05 11:45