首页  >  问答  >  正文

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]

目前有点不知道要如何处理了。还请各位高手多帮忙!感谢感谢!

过去多啦不再A梦过去多啦不再A梦2702 天前1049

全部回复(1)我来回复

  • 某草草

    某草草2017-05-27 17:43:27

    @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进行查询

    回复
    0
  • 取消回复