0

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
}
Yuki Hashimoto
  • 1,013
  • 7
  • 19
  • Does this answer your question? [Retrieving Polymorphic Hibernate Objects Using a Criteria Query](https://stackoverflow.com/questions/3199523/retrieving-polymorphic-hibernate-objects-using-a-criteria-query) – cbbs Dec 14 '20 at 06:07
  • No, the Q&A does not deal "GROUP BY" clause. – Yuki Hashimoto Dec 15 '20 at 02:15

0 Answers0