您的位置:首頁技術文章
文章詳情頁

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

瀏覽:100日期:2023-11-15 16:41:20

問題描述

大家好,目前有個需求就是求表中兩列的和,然后將結果排序。詳細描述如下:我的表里有兩列是費用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]

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

問題解答

回答1:

@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進行查詢

標簽: java
相關文章:
国产综合久久一区二区三区