使用 DbUnit 将 BigDecimal 数据放入 HSQLDB 测试数据库
我在后端使用 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 似乎出于某种原因对我定义的数字进行了四舍五入。有什么办法可以强制我不发生这种情况吗?谁能解释为什么它会这样做?
谢谢!
I'm using Hibernate JPA in my backend. I am writing a unit test using JUnit and DBUnit to insert a set of data into an in-memory HSQL database.
My dataset contains:
<order_line order_line_id="1" quantity="2" discount_price="0.3"/>
Which maps to an OrderLine Java object where the discount_price column is defined as:
@Column(name = "discount_price", precision = 12, scale = 2)
private BigDecimal discountPrice;
However, when I run my test case and assert that the discount price returned equals 0.3, the assertion fails and says that the stored value is 0. If I change the discount_price in the dataset to be 0.9, it rounds up to 1.
I've checked to make sure HSQLDB isn't doing the rounding and it definitely isn't because I can insert an order line object using Java code with a value like 5.3 and it works fine.
To me, it seems like DBUtils is for some reason rounding the number I've defined. Is there a way I can force this to not happen? Can anyone explain why it might be doing this?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我遇到了这个问题,因为我正在与类似的问题作斗争,不幸的是,在我当前的设置中,我无法升级休眠。所以我找到了一个不同的解决方案来解决问题,而不需要升级休眠。认为分享不会有坏处。您只需扩展 HSQLDialect
I ran across this question since I was fighting with a similar problem, unfortunately in my current setup I'm not able to upgrade hibernate. So I found a different solution which fixes the problem without the need to upgrade hibernate. Thought it would not harm to share. You simply extend the HSQLDialect
我有一个类似的问题...通过升级到 Hibernate 3.6 修复
I had a similar problem...fixed by upgrading to to Hibernate 3.6
我使用 DbUnit 2.2.2、HSQLDB 1.8.0.10 和 Hibernate EM 3.4.0.GA 进行了快速测试,一切按预期工作:
discount_price
列中输入numeric(12,2)
discount_price
值读取到BigDecimaldiscountPrice
中,assertEquals(0.3d, orderLine. getDiscountPrice().doubleValue())
通过所以我的问题是:
I did a quick test on my side with DbUnit 2.2.2, HSQLDB 1.8.0.10 and Hibernate EM 3.4.0.GA and things are working as expected:
discount_price
column of typenumeric(12,2)
discount_price
values into aBigDecimal discountPrice
assertEquals(0.3d, orderLine.getDiscountPrice().doubleValue())
passesSo my questions are: