繁体   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