MonetaryAmountType should respect fractions digits when persisting to database
See original GitHub issueSome country don’t have any fraction digit. and when persisting should only store the whole number.
I fixed it by explicitly set the scale on getPropertyValue on https://github.com/vladmihalcea/hibernate-types/blob/7b022294534a9a109cc4749fd7ab9affdfd21d59/hibernate-types-60/src/main/java/com/vladmihalcea/hibernate/type/money/MonetaryAmountType.java#L34
My fix:
@Override
public Object getPropertyValue(MonetaryAmount component, int property) throws HibernateException {
// alphabetical (amount, currency)
switch (property) {
case 0:
return component
.getNumber()
.numberValue(BigDecimal.class)
.setScale(
component.getCurrency().getDefaultFractionDigits(),
RoundingMode.HALF_UP
);
case 1:
return component.getCurrency().getCurrencyCode();
}
return null;
}
`
Issue Analytics
- State:
- Created a year ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Money data on PostgreSQL using Java - Stack Overflow
If working in a system without good support for accurate fractional numbers, this approach might be an acceptable workaround.
Read more >2. SQL Data Types
An interval of days and seconds with n digits of precision in the fractions of seconds. Abstract. MONEY. MONEY. Monetary amount in the...
Read more >Chapter 5. Basic O/R Mapping - NHibernate
We will mainly describe the document elements and attributes that are used by NHibernate at runtime. The mapping document also contains some extra...
Read more >Hibernate Validator 8.0.0.Final - Jakarta Bean Validation ...
This chapter will show you how to get started with Hibernate ... is a number having up to integer digits and fraction fractional...
Read more >Statements of Actuarial Opinion On Property and Casualty ...
monitors and advises on activities as respects financial reporting ... narrative and required Exhibits, shall be in the format of and.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@stwonary Don’t worry about it. When I have some time, I’ll take your integration test case and see if I can provide a fix for it. Hopefully, in November, I’ll have some time to review it.
I tried to implement for Hibernate 5.2 and Hibernate 5.5 without success. For some reason on those versions the values returned by
getPropertyValueare totally ignored.I hardcoded
BigDecimal.valueOf(1)ingetPropertyValue. The method is called but the valued persisted is still from the originalmonetaryAmount.getNumber().numberValue(BigDecimal.class)any ideas?