Mapping result of aggregate query to hibernate object

筅森魡賤 提交于 2019-12-11 04:05:33

问题


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

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---
}

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

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.


回答1:


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;
}

Also, there are some examples here.



来源:https://stackoverflow.com/questions/4634820/mapping-result-of-aggregate-query-to-hibernate-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!