I want to apply aggregation function grouping by the DiscriminatorColumn
in JPQL.
For example, I want to calculate the sum of calories for each of "grain", "vegetable", ...., which are subclasses of "food" (the code snippet is given below).
I can calculate that by native sql, i.e.SELECT food_type, SUM(calorie) FROM food GROUP BY food_type;
. But for inter-database compatibility I want to write it in JPQL.
Do you have any suggestions? Thanks!
@Entity
@Inheritance
@DiscriminatorColumn(name = "FOOD_TYPE")
@Table
public abstract class Food {
@Id
Long foodId;
@Column
String name;
@Column
Double calorie;
}
@Entity
@DiscriminatorValue("GRAIN")
public class Grain extends Food {
// some fields
}
@Entity
@DiscriminatorValue("VEGETABLE")
public class Vegetable extends Food {
// some fields
}