开发者

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

开发者 https://www.devze.com 2023-04-07 04:50 出处:网络
After going through the previous develop\'s trainwreck of code I realized I need to move all of the money base开发者_运维问答d columns to not use floating point math.On the Java side this means using

After going through the previous develop's trainwreck of code I realized I need to move all of the money base开发者_运维问答d 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?


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号