문제

How to use order by clause for sort by sum of field

Entity:

    class MyClass {

        private int a;
        private int b;

        //...

    }

HQL works correctly:

"SELECT myclass FROM MyClass myclass ORDER BY myclass.a + myclass.b DESC "

How to do the same thing using Criteria API?

도움이 되었습니까?

해결책

As you said you could use @Formula

@Column(name="column_a")
private Integer a;
@Column(name="column_b")
private Integer b;
@Formula("(column_a+column_b)")
private Integer c;

After that you can sort by property c like

criteria.addOrder(Order.asc("c"));
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top