Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using sum() in hibernate criteria

How can I write the sql query select sum(amount * direction) from transactions into hibernate criteria ?

like image 741
panagdu Avatar asked Jan 07 '11 11:01

panagdu


1 Answers

Think I found the answer. You must use the Projection.sqlProjection() and not Projections.sum(). Example:

.setProjection(Projections.sqlProjection("sum(cast(amount as signed)* direction) as amntDir", new String[] {"amntDir"} , new Type[] {Hibernate.DOUBLE}));
like image 71
panagdu Avatar answered Oct 22 '22 02:10

panagdu