你会在MySQL中使用Java / Hibernate映射BigDecimal的类型是什么? [英] What type would you map BigDecimal in Java/Hibernate in MySQL?

查看:1329
本文介绍了你会在MySQL中使用Java / Hibernate映射BigDecimal的类型是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在经历了之前的开发中的代码故障之后,我意识到我需要移动所有基于钱的列来不使用浮点数学。在Java方面,这意味着使用BigDecimal,但是当使用Hibernate / JPA和MySQL 5时,什么是适合于创建该列的MySQL数据类型?

解决方案

DECIMAL和NUMERIC。


DECIMAL和NUMERIC类型推荐的Java映射是
java.math.BigDecimal。 java.math.BigDecimal类型提供了数学
操作,以允许BigDecimal类型被添加,减去,
相乘,并且被其他BigDecimal类型,整数
类型和浮点类型。

建议用于检索DECIMAL和NUMERIC值的方法是
ResultSet.getBigDecimal。 JDBC还允许以简单字符串或字符数组的形式访问这些SQL类型
。因此,Java程序员可以使用
getString来接收DECIMAL或NUMERIC结果。但是,这使得
是DECIMAL或NUMERIC用于货币值的常见情况,因为这意味着应用程序编写者必须
对字符串执行数学运算。也可以将这些SQL
类型检索为任何Java数字类型。


查看这里了解更多详情。

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?

解决方案

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.

这篇关于你会在MySQL中使用Java / Hibernate映射BigDecimal的类型是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆