Search code examples
jpajql

Get counts of values of an Enum in JQL


Is thare any way a JQL can get the counts according to the values of an Enum? For example, Enum has values as Single, Married, Divorced, etc. and get the counts of Persons Entity in a single Java Persistence Language Query?


Solution

  • There are two ways, either use a group by, or sub-selects in the Select clause,

    Select Count(p), p.status from Person p group by p.status
    

    or,

    Select (Select Count(p) from Person p where p.status = Status.Single), (Select Count(p) from Person p where p.status = Status.Married), (Select Count(p) from Person p where p.status = Status.Divorced)
    

    but sub-selects in the Select clause is not supported by JPA (at least JPA 2.0, 2.1 does I believe), EclipseLink 2.4 or greater does support this.