Search code examples
javadatabasehibernatehqlcriteria

HQL: Division inside Criteria Query


This doesn't work in Hibernate, because sum seems to be only able to take in one property: criteria.setProjection(Projections.sum("h.dogs/h.cats"));

How do I rewrite this, without using any native SQL, as a criteria Projection or the like? If I just put h.dogs inside the sum, then that works but I need to divide them.


Solution

  • I found a good solution.

    @Formula("dogs / cats")
    private double calculation;
    

    in my Object class did the trick, I can then refer to it via

    criteria.setProjection(Projections.sum("h.calculation"));