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. phpStudy2017轻巧版mysql无法启动2. centos7 编译安装 Python 3.5.1 失败3. 为什么我输入了refresh不会跳转?请教大神支招!4. list - python 求助5. python - 《flask web 开发》一书,数据库中多对多关系的实现问题?6. python3.x - python连oanda的模拟交易api获取json问题第二问7. flask - python web中如何共享登录状态?8. 为什么我写的PHP不行9. python3.x - python3.5使用pyinstaller打包报错找不到libpython3.5mu.so.1.0等文件求解?10. mysql - 无耻的请教一条sql
