簡體   English   中英

嘗試按JPA Spring Boot中的嵌套字段排序時,OrderBy不起作用

[英]OrderBy not working while trying to sort by a nested field in JPA Spring Boot

我有這樣的實體

@Entity
@Table(name = "past_price")
public class PastPrice {

    @Id
    private String symbol;
    @OneToMany(fetch = FetchType.EAGER,cascade = CascadeType.ALL)
    @JoinColumn(name = "symbol")
    private Set<Price> prices = null;

    public String getSymbol() {
        return symbol;
    }

    public void setSymbol(String symbol) {
        this.symbol = symbol;
    }

    public Set<Price> getPrices() {
        return prices;
    }

    public void setPrices(Set<Price> prices) {
        this.prices = prices;
    }

}
@Entity
public class Price {
    @Id
    @Temporal(TemporalType.TIMESTAMP)
    private Date date;
    private String price;

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

}

然后我有一個像這樣的JPA存儲庫

@Repository
public interface PastPriceRepo extends JpaRepository<PastPrice,String> {
    PastPrice findBySymbol_OrderByPricesDate(String symbol);
}

當我試圖通過對它進行排序date是內部Price類,這是內部PastPrice類。 但是我沒有得到排序的列表。 我從restcontroller調用它時得到了這個。

{
    "symbol": "SAX",
    "prices": [
        {
            "date": 1552295163000,
            "price": "234.00"
        },
        {
            "date": 1552459623000,
            "price": "236.00"
        },
        {
            "date": 1552470475000,
            "price": "232.00"
        },
        {
            "date": 1553155398000,
            "price": "233.00"
        },
        {
            "date": 1553762003000,
            "price": "234.00"
        },
        {
            "date": 1552469784000,
            "price": "233.00"
        },
        {
            "date": 1553755597000,
            "price": "235.00"
        },
        {
            "date": 1553760225000,
            "price": "234.00"
        },
        {
            "date": 1552284839000,
            "price": "226.00"
        }
    ]
}

我應該如何在Repository接口中組成我的方法? 任何幫助,將不勝感激。 謝謝

你可以做類似的事情

@Repository
public interface PastPriceRepo extends JpaRepository<PastPrice,String> {
    @Query(
        value = "select pp from PastPrice pp join pp.prices ps where pp.symbol=:symbol Order By ps.date desc"
    )
    PastPrice findBySymbol_OrderByPricesDate(@Param("symbol") String symbol);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM