El Guapo
El Guapo

Reputation: 5781

Mapping result of aggregate query to hibernate object

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.

Upvotes: 2

Views: 6636

Answers (1)

zinan.yumak
zinan.yumak

Reputation: 1590

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.

Upvotes: 6

Related Questions