Home  >  Q&A  >  body text

How to sort results of sum of two columns using spring data jpa

Hello everyone, there is currently a need to find the sum of two columns in the table, and then sort the results. The detailed description is as follows:
There are two columns in my table: fee a (pay_a) and fee b (pay_b). The current requirement is to add the two and sum them (select (pay_a pay_b) from paytable), Then the results are sorted (the database uses oracle).

The framework I use is spring data jpa. When using sort, I found that sort cannot parse the addition expression. There is no such member in the prompt table.
Change the method, use @Transient to declare a new member payall in the entity (there is no such column in the database table), and define getPayall()
{

return pay_a + pay_b;

}
Then in sort, use "payall" to sort, and the result is an error.
Unable to locate Attribute with the given name [payall] on this ManagedType [io.koala.tobacco. dataservice.entity.PotentialCust]

I don’t know how to deal with it now. Please help all the experts! Thank you!

过去多啦不再A梦过去多啦不再A梦2702 days ago1053

reply all(1)I'll reply

  • 某草草

    某草草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 Use native sql to query

    reply
    0
  • Cancelreply