如何按不同的连接 table 列动态排序

How to dynamically sort by different joined table column

public interface InvoiceRepository extends JpaRepository<Invoice, Long> {
    @Query("SELECT DISTINCT NEW com.invoice.InvoiceResult"
            + "(i.invoiceId, t.vendor, i.totalTrips, i.fromDate, i.toDate, i.totalFare, i.paidAmount, i.status, i.createdDate, i.lastModifiedDate)"
            + " FROM Invoice i"
            + " JOIN i.trips t")
    List<InvoiceResult> retrieveInvoices(Sort sort);

}
String sortField = "";
Sort.by(sortField).ascending();

sortField 的值应该是多少才能按以下所选列进行排序?

i.invoiceId、t.vendor、i.totalTrips、i.fromDate、i.toDate、i.totalFare、i.paidAmount、i.status、i.createdDate、i.lastModifiedDate

如果我通过 i.invoiceId,我会得到以下错误

org.hibernate.QueryException: 无法解析 属性: i of: com.seamless.one.billing.domain.Invoice [SELECT 不同的新 com.seamless.one.billing.pojo.InvoiceResult(i.invoiceId, t.vendor, i.totalTrips, i.fromDate, i.toDate, i.totalFare, i.paidAmount, i.status, i.createdDate, i.lastModifiedDate) FROM com.seamless.one.billing.domain.Invoice i JOIN i.trips t WHERE t.vendor = :vendor order by i.i.invoiceId asc];嵌套异常是 java.lang.IllegalArgumentException: org.hibernate.QueryException: 无法解析 属性: i of: com.seamless.one.billing.domain.Invoice [SELECT 不同的新 com.seamless.one.billing.pojo.InvoiceResult(i.invoiceId, t.vendor, i.totalTrips, i.fromDate, i.toDate, i.totalFare, i.paidAmount, i.status, i.createdDate, i.lastModifiedDate) FROM com.seamless.one.billing.domain.Invoice i JOIN i.trips t WHERE t.vendor = :vendor order by i.i.invoiceId asc]

我会选择 lastModifiedDatecreatedDate 列并调用 降序 顺序,因为我想首先获得新发票

你非常接近,如果你在没有 i. 的情况下编写它应该可以工作。

Sort.by(Sort.Direction.ASC, "invoiceId");

在错误的最后一行,您甚至可以看到,创建的查询使用 i.i.invoiceId 对您的查询进行排序,这显然 i. 太多了。