您List<Address>
添加@OrderBy
標註在您的域。
@OneToMany(...)
@OrderBy("city")
private List<Address> addresses;
編輯:
它可以同時獲得ASC和DESC根據城市單個查詢訂購地址。首先,你需要在地址標註@OrderBy("city DESC")
這樣的:
@OneToMany(...)
@OrderBy("city DESC")
private List<Address> addresses;
之後,你需要創建2吸氣劑是這樣的:
// this simply get address which is in descending order
public List<Address> getAddressesWithCitiesSortedInDesc() {
return addresses;
}
// this will make descending ordered address to ascending order
public List<Address> getAddressesWithCitiesSortedInAsc() {
Collections.sort(addresses, new Comparator<Address>(){
public int compare(Address a1, Address a2){
return a1.getCity().compareTo(a2.getCity());
}
});
return addresses;
}
如果調用findAll()
方法,你會得到兩個JSON像用戶這樣的:
"addressesWithCitiesSortedInAsc": [
{..},
{..}
],
"addressesWithCitiesSortedInDesc": [
{..},
{..}
]
能否請您分享您的用戶域類代碼 – CIPHER007