我在后端使用 Hibernate JPA。我正在使用 JUnit 和 DBUnit 编写单元测试,将一组数据插入到内存 HSQL 数据库中。
我的数据集包含:
<order_line order_line_id="1" quantity="2" discount_price="0.3"/>
它映射到一个 OrderLine Java 对象,其中 discount_price 列定义为:
@Column(name = "discount_price", precision = 12, scale = 2)
private BigDecimal discountPrice;
但是,当我运行我的测试用例并断言返回的折扣价格等于 0.3 时,断言失败并说存储的值为 0。如果我将数据集中的 discount_price 更改为 0.9,它会四舍五入为 1。
我已经检查以确保 HSQLDB 没有进行四舍五入,这绝对不是因为我可以使用 Java 代码插入一个订单行对象,其值类似于 5.3,它工作正常。
对我来说,似乎 DBUtils 出于某种原因将我定义的数字四舍五入。有没有办法可以强制这种情况不发生?谁能解释它为什么会这样做?
谢谢!