What type would you map BigDecimal in Java/Hibernate in MySQL?

50,408

DECIMAL and NUMERIC.

The recommended Java mapping for the DECIMAL and NUMERIC types is java.math.BigDecimal. The java.math.BigDecimal type provides math operations to allow BigDecimal types to be added, subtracted, multiplied, and divided with other BigDecimal types, with integer types, and with floating point types.

The method recommended for retrieving DECIMAL and NUMERIC values is ResultSet.getBigDecimal. JDBC also allows access to these SQL types as simple Strings or arrays of char. Thus, Java programmers can use getString to receive a DECIMAL or NUMERIC result. However, this makes the common case where DECIMAL or NUMERIC are used for currency values rather awkward, since it means that application writers have to perform math on strings. It is also possible to retrieve these SQL types as any of the Java numeric types.

Have a look here for further details.

Share:
50,408
benstpierre
Author by

benstpierre

My entire career has been building commercial software mostly in Java and C#. I have working with a dozen other languages but I really love working in these two the most. Recently I have been writing application in Vaadin and think it's the best thing since sliced bread.

Updated on August 13, 2020

Comments

  • benstpierre
    benstpierre over 3 years

    After going through the previous develop's trainwreck of code I realized I need to move all of the money based columns to not use floating point math. On the Java side this means using BigDecimal but when using Hibernate/JPA and MySQL 5 what would be the appropriate MySQL data type to make that column?