假設你有以下實體:Spring Data JPA - 是否可以對計算出的屬性進行排序?
@Entity
public class Game {
@Id
@GeneratedValue
private Integer id;
private String name;
private Calendar startTime;
private int durationInSeconds;
public GameStatus getStatus() {
if(startTime.after(Calendar.getInstance()))
{
return GameStatus.SCHEDULED;
} else {
Calendar endTime = Calendar.getInstance();
endTime.setTime(startTime.getTime());
endTime.roll(Calendar.SECOND, durationInSeconds);
if(endTime.after(Calendar.getInstance())) {
return GameStatus.OPEN_FOR_PLAY;
}
else {
return GameStatus.FINISHED;
}
}
}
}
如果我GameRepository
是PagingAndSortingRepository
,我怎樣才能得到一個網頁的結果,由status
屬性排序?
我目前得到:
java.lang.IllegalArgumentException: Unable to locate Attribute with the the
given name [status] on this ManagedType [org.test.model.Game]
,我可以因爲status
理解確實沒有JPA屬性。有沒有解決的辦法?
(我使用Hibernate的下方,所以任何特定的Hibernate也行)
也許更換你getStatus方法的一個公式,請參閱http://stackoverflow.com/a/29625652/180100 –