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

前端 未结 1 1637
野性不改
野性不改 2021-02-02 07:15

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 me

1条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-02 07:48

    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.

    0 讨论(0)
提交回复
热议问题