Reputation: 4022
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!
Upvotes: 5
Views: 3238
Reputation: 5728
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)" );
}
}
Upvotes: 3
Reputation: 570315
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 type numeric(12,2)
discount_price
values into a BigDecimal discountPrice
assertEquals(0.3d, orderLine.getDiscountPrice().doubleValue())
passesSo my questions are:
Upvotes: -1