Spring Data JPA – Is it possible to sort on a calculated property?

The problem is that Spring Data’s PageRequest sort is done on the database layer by forming the ORDER BY clause.

You could create a @Formula column, e.g.

@Entity
public class Game {
...
     // rewrite your logic here in HQL
     @Formula("case when startTime >= endTime then 'FINISHED' ... end")
     private String status;

Then it will be possible to use the new column in sort order as everything you write in the formula will be passed to ORDER BY clause.

Leave a Comment