If I set, say, 10.244 to a column of type DECIMAL(20,2)
, the actual saved value would be 10.24. If I set 10.245, 10.25 will be saved.
It makes sense. However, I'm curious:
It appears it's not JDBC, at least judging from Hibernate logs. Note 11.264. 11.26 was actually saved.
Hibernate:
update
account
set
balance=?,
initial_balance=?,
user_id=?
where
id=?
2025-05-30T14:37:17.472+03:00 TRACE 11240 --- [pool-2-thread-1] org.hibernate.orm.jdbc.bind : binding parameter (1:NUMERIC) <- [11.264]
2025-05-30T14:37:17.473+03:00 TRACE 11240 --- [pool-2-thread-1] org.hibernate.orm.jdbc.bind : binding parameter (2:NUMERIC) <- [10.24]
2025-05-30T14:37:17.474+03:00 TRACE 11240 --- [pool-2-thread-1] org.hibernate.orm.jdbc.bind : binding parameter (3:BIGINT) <- [15]
2025-05-30T14:37:17.475+03:00 TRACE 11240 --- [pool-2-thread-1] org.hibernate.orm.jdbc.bind : binding parameter (4:BIGINT) <- [1]
The value is represented as java.math.BigDecimal
on the backend. I realize I can manually call bigDecimal.setScale(2, java.math.RoundingMode.DOWN)
before trying to persist. It is not what I am asking.
trigger
that swoops in and re-writes your values whenever necessary. Technically, you could also build a whole new numeric type of your own and make it do that internally but please don't.