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]
目前有點不知道要如何處理了。還請各位高手多幫忙!感謝感謝!
問題解答
回答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進行查詢
相關文章:
1. 對mysql某個字段監控的功能2. html - vue項目中用到了elementUI問題3. javascript - HTML字符串排版4. javascript - table列過多,有什么插件可以提供列排序和選擇顯示列的功能5. python - 為什么正常輸出中文沒有亂碼,zip函數之后出現中文編程unicode編碼的問題,我是遍歷輸出的啊。6. showpassword里的this 是什么意思?代表哪個元素7. javascript - js中向下取整8. javascript - windows下如何使用babel,遇到了困惑9. html5 - ElementUI table中el-table-column怎么設置百分比顯示。10. JavaScript事件
