您的位置:首页技术文章
文章详情页

java - 如何使用spring data jpa进行两个列和的结果排序

【字号: 日期:2023-11-15 16:41:20浏览:52作者:猪猪

问题描述

大家好,目前有个需求就是求表中两列的和,然后将结果排序。详细描述如下:我的表里有两列是费用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
相关文章: