在休眠条件中使用 sum()

2022-09-02 13:25:46

如何将 sql 查询写入休眠条件?select sum(amount * direction) from transactions


答案 1

想想我找到了答案。必须使用 Projection.sqlProjection() 而不是 Projections.sum()。例:

.setProjection(Projections.sqlProjection("sum(cast(amount as signed)* direction) as amntDir", new String[] {"amntDir"} , new Type[] {Hibernate.DOUBLE}));

答案 2

我认为你需要的是公式。像这样的东西,

@Entity
@Table('TRANSACTIONS')
Class transactions {

     @Column("AMOUNT")
     private double amount;

     @Column("DIRECTION")
     private double direction;

     @Formula("AMOUNT * DIRECTION")
     private double multiplication;

}

并将乘法列添加到投影列表中。


推荐