Search code examples
javahibernatehibernate-criteria

row multiplication in Hibernate Criteria


How to implement the following SQL query using Hibernate Criteria API:

SELECT
    SUM(field1) as s,
    SUM(field1 * field2) as m
FROM 
    table

Solution

  • Try

    add(Projections.sqlProjection("sum(field1)) AS s", new String[] { "s" }, new Type[] {Hibernate.DOUBLE }))
    add(Projections.sqlProjection("sum(field1*field2) AS m", new String[] { "m" }, new Type[] {Hibernate.DOUBLE }))