使用 DbUnit 将 BigDecimal 数据放入 HSQLDB 测试数据库

发布于 2024-09-02 18:22:12 字数 618 浏览 8 评论 0原文

我在后端使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤千羽 2024-09-09 18:22:12

我遇到了这个问题,因为我正在与类似的问题作斗争,不幸的是,在我当前的设置中,我无法升级休眠。所以我找到了一个不同的解决方案来解决问题,而不需要升级休眠。认为分享不会有坏处。您只需扩展 HSQLDialect

public class HSQLNumericWithPrecisionDialect extends HSQLDialect {
    public HSQLNumericWithPrecisionDialect() {
        super();
        registerColumnType( Types.NUMERIC, "numeric($p, $s)" );
    }
}

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

public class HSQLNumericWithPrecisionDialect extends HSQLDialect {
    public HSQLNumericWithPrecisionDialect() {
        super();
        registerColumnType( Types.NUMERIC, "numeric($p, $s)" );
    }
}
口干舌燥 2024-09-09 18:22:12

我有一个类似的问题...通过升级到 Hibernate 3.6 修复

I had a similar problem...fixed by upgrading to to Hibernate 3.6

对不⑦ 2024-09-09 18:22:12

我使用 DbUnit 2.2.2、HSQLDB 1.8.0.10 和 Hibernate EM 3.4.0.GA 进行了快速测试,一切按预期工作:

  • DbUnit 正确地将十进制值插入到 discount_price 列中输入 numeric(12,2)
  • Hibernate 正确地将 discount_price 值读取到 BigDecimaldiscountPrice 中,
  • 如下 assertEquals(0.3d, orderLine. getDiscountPrice().doubleValue()) 通过

所以我的问题是:

  • 您到底使用什么版本的 DbUnit?
  • 你到底如何做你的断言?你们也有 DbUnit API 吗?

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:

  • DbUnit correctly inserts decimal values into a discount_price column of type numeric(12,2)
  • Hibernate correctly reads the discount_price values into a BigDecimal discountPrice
  • the following assertEquals(0.3d, orderLine.getDiscountPrice().doubleValue()) passes

So my questions are:

  • What version of DbUnit do you use exactly?
  • How do you do your assert exactly? Do you DbUnit API for that too?
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文