java - 你会在 MySQL 中映射 Java/Hibernate 中的 BigDecimal 什么类型?

标签 java mysql hibernate numbers bigdecimal

在经历了之前开发的大量代码之后,我意识到我需要移动所有基于金钱的列以不使用 float 学。在 Java 方面,这意味着使用 BigDecimal,但是当使用 Hibernate/JPA 和 MySQL 5 时,生成该列的适当 MySQL 数据类型是什么?

最佳答案

十进制和数字。

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.

看看here了解更多详情。

关于java - 你会在 MySQL 中映射 Java/Hibernate 中的 BigDecimal 什么类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7550337/

相关文章:

java - 如何在 Java 中将 "HH:MM"格式字符串转换为时间戳?

sql - 如何查看 MySQL 数据库/表/列的字符集是什么?

mysql - 从相关表(模型)中获取数据sequelize

java - Hibernate 生成预定义的实体标识符

java - 构建项目时如何通过maven更改hibernate.cfg.xml文件路径?

java - 从命令行编译带有两个 jar 的 java 文件?

java - 在 Java 中恢复使用 mysqldump 创建的备份文件时出错

java - 处理时间加倍的函数存在问题

java - 为什么 EntityManager 关闭了?

java - 如何仅更新字段的子集并更新存储库?