I have an entity, say
@Entity
public class Category {
private String name;
private int usersInCategory;
}
I want to fill usersInCategory field with some aggregating SQL query (select count(*) ... group by ...).
How can I do this in Hibernate?
Use a
Formula(this is an Hibernate specific annotation). From the documentation:As the documentation writes, the SQL fragment can be pretty complex and can reference the owning entity like in the example below (the non aliased
idcolumn in theo.customer_id=idpart of the where clause references the owning entity column):See also