聚合查询映射到hibernate对象的结果 [英] Mapping result of aggregate query to hibernate object

查看:155
本文介绍了聚合查询映射到hibernate对象的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将聚合查询的结果映射到hibernate支持的域对象中的字段?

Is it possible to map the result of an aggregate query to a field in a hibernate-backed domain object?

例如:如果我有一个Car对象如下所示 -

For example: If I have a Car object that looks like the following --

@Entity
public class Car {
    @Id 
    private int id;
    @Column 
    private String carName;
    private int carCount;
    ---Getters/Setters---
}

我想carCount字段/属性是我持久性存储中所有汽车的总数,这可能吗?

I would like the carCount field/property to be the total count of all the cars in my persistence store, is this possible?

我查看了Hibernate文档,我可以运行查询,但是我没有看到可以将该值设置为carCount的位置。

I've looked at the Hibernate documentation, I can run the query, but I don't see where I can set that value to the "carCount"

谢谢。

Thanks.

推荐答案



You can make it with formula. Something like,

@Entity
public class Car {
    @Id 
    private int id;
    @Column 
    private String carName;

    @Formula("select count(*) from Car c where c.id = id")
    private int carCount;
}

另外,还有一些例子这里

这篇关于聚合查询映射到hibernate对象的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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