Hibernate loss of precision in results when mapping a number (22,21) to BigDecimal

雨燕双飞 提交于 2019-12-19 08:04:51

问题


I have this column in my Oracle 11g mapped as NUMBER (21,20), which is mapped in Hibernate as:

@Column(name = "PESO", precision = 21, scale = 20, nullable = false)
public BigDecimal getWeight() {
    return weight;
}

For a particular record for which the value of the column is 0.493 I get a BigDecimal whose value is 0.49299999999. It seems that somewhere there is a loss of precision due (maybe) to a Double or Float conversion, but I couldn't track it down with a simple unit test like this:

Double d = new Double("0.493");
System.out.println((d));

Any variant of that code, using Float, BigDecimal and various constructors gives the same result: "0.493"... Any hint on how should I map the column to avoid such issues? I'm using Hibernate 3.5.6, with JPA annotations and Hibernate API (that is Session and not EntityManager)


回答1:


It's a result of initializing BigDecimal from double:

System.out.println(String.format("%21.20f", new BigDecimal(0.493)); 
// Prints 0,49299999999999999378  

So, when BigDecimal initialized this way is saved in the database, it produces an inaccurate value, which is correctly loaded later.

If BigDecimal is initialized by string or if the value is set directly in Java everything works fine.



来源:https://stackoverflow.com/questions/5734036/hibernate-loss-of-precision-in-results-when-mapping-a-number-22-21-to-bigdecim

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!