开发者

Putting BigDecimal data into HSQLDB test database using DbUnit

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 o开发者_JS百科bject 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!


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)" );
    }
}


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


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?
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜