Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define BigDecimal rounding strategy for BigDecimal in Spring Data JPA?

I'm trying to persist payment amount field as Big Decimal via Spring Data JPA (Hibernate). I have define column like this:

@Column(precision = 19, scale = 3)

but the problem I'm facing is that the the UP strategy is being used when rounding the number while persisting. For example, if i try to persist 0.12345 it will save 0.124.

I need the DOWN rounding strategy in this situation and i don't know how to set this. I can probably set the scale and rounding strategy for my big decimal in constructor/setters but that doesn't seem like good option to me. Is there some annotation or argument which i need to pass to my jpa so it uses the down strategy while rounding?

like image 499
Nikola Obradovic Avatar asked Sep 17 '25 14:09

Nikola Obradovic


1 Answers

You can look here BigDecimal with JPA and flexible scale for some solution approach. I don't think it can be controlled with some property. I would say it's also not something that belongs on the domain layer, as it's a "business logic" to define how the prices are rounded.

like image 109
Thomas Avatar answered Sep 19 '25 04:09

Thomas