首頁  >  問答  >  主體

java - 如何使用spring data jpa進行兩個欄位和的結果排序

大家好,目前有個需求就是求表中兩列的和,然後將結果排序。詳細描述如下:
我的表裡有兩列是費用a(pay_a)和費用b(pay_b),目前的需求是要將這兩者相加求和(select (pay_a pay_b)from paytable),然後結果排序(資料庫使用的是oracle)。

我使用的框架是spring data jpa。使用sort時,發現sort無法解析相加的表達式。提示表中無此成員。
換了個方法,在entity中用@Transient宣告一個新成員payall(資料庫表中無此列),定義getPayall()
{

return pay_a + pay_b;

}
然後在sort中,使用」payall「進行排序,結果運行錯誤,會提示
Unable to locate Attribute with the the given name [payall] on this ManagedType [io.koala.tobacco. dataservice.entity.PotentialCust]

目前有點不知道要如何處理了。還請各位高手多幫忙!感謝感謝!

过去多啦不再A梦过去多啦不再A梦2702 天前1048

全部回覆(1)我來回復

  • 某草草

    某草草2017-05-27 17:43:27

    @Query(value = "select new test.Account(a.price1 + a.price2) from Account a", nativeQuery = true)
    Account testSum();
    package test;
    public class Account {
        public Account(price1, price2) {
        }
    }

    nativeQuery = true 使用原生的sql來查詢

    回覆
    0
  • 取消回覆