question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MonetaryAmountType should respect fractions digits when persisting to database

See original GitHub issue

Some 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:open
  • Created a year ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
vladmihalceacommented, Oct 17, 2022

@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.

0reactions
stwonarycommented, Oct 17, 2022

I tried to implement for Hibernate 5.2 and Hibernate 5.5 without success. For some reason on those versions the values returned by getPropertyValue are totally ignored.

I hardcoded BigDecimal.valueOf(1) in getPropertyValue. The method is called but the valued persisted is still from the original monetaryAmount.getNumber().numberValue(BigDecimal.class)

any ideas?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found