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. JAVASCRIPT 构造函数和原型互相指向问题?求助!!!2. css - 移动端 oppo 手机之 Border-radius3. html5 - 移动端UC浏览器兼容问题4. android - 具体的空指针是在哪????5. javascript - 前端富文本编辑器 wangeditor 如何展示富文本?6. docker pull 错误?7. 关于《Effective Java中文版(第2版)》的问题8. mysql 创建root 用户出错,这是什么原因?mysql 中也没有root用户9. python - 请问matplotlib.pyplot.save的路径如何更改10. angular.js - ionic 弹窗样式