使用休眠条件选择值对象 [英] using hibernate criteria to select into value objects

查看:53
本文介绍了使用休眠条件选择值对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Hibernate Criteria API来执行以下HQL:

I'm trying to find out if it is possible to do the following HQL using the Hibernate Criteria API:

String hql = "select new InitialCount(substring(name, 1,1), count(id)) from Person group by substring(name, 1,1)";

InitialCount是一个非常简单的bean,具有字符串和长构造函数:

Where InitialCount is a very simple bean with a string and long contructor:

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();        


其中FormulaProp是使用公式"substring(name,1,1)"映射的人员的属性


where formulaProp is a property of person mapped with the formula "substring(name, 1,1)"

这篇关于使用休眠条件选择值对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆