Anto Robinson
Anto Robinson

Reputation: 463

How to limit decimal precision in hibernate

My SQL query is "SELECT productid,FORMAT(AVG(score),2) FROM product GROUP BY productid"

I am getting output as

1001 : 2.23, 1002 : -2.69

In Hibernate my query will be "SELECT product.productId,FORMAT(AVG(product.score),2) FROM ProductDAO product GROUP BY product.productId"

But the format function not supported in hibernate, is there any way to achieve decimal precision in hibernate?

Upvotes: 0

Views: 647

Answers (1)

Sudhir Mane
Sudhir Mane

Reputation: 306

In Hibernate you can achieve the decimal precision by specifying the precision & scale property in hbm mapping.

But in your case as you are using AVG function, hence above approach will not work. You can collect the value of AVG(score) in BigDecimal & use setScale method of the BigDecimal.

 aNumber.setScale(2, RoundingMode.HALF_UP);

Upvotes: 1

Related Questions