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

前端 未结 1 585
你的背包
你的背包 2021-01-13 07:37

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          


        
相关标签:
1条回答
  • 2021-01-13 08:18

    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.

    0 讨论(0)
提交回复
热议问题