如何在Hibernate中映射BigDecimal,以便恢复与我放入的相同比例? [英] How do I map a BigDecimal in Hibernate so I get back the same scale I put in?

查看:117
本文介绍了如何在Hibernate中映射BigDecimal,以便恢复与我放入的相同比例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中, new BigDecimal(1.0)!= new BigDecimal(1.00) scale matter

然而,这显然不适用于Hibernate / SQL Server。如果我将BigDecimal的缩放比例设置为特定值,请通过Hibernate将BigDecimal保存到数据库,然后重新对对象进行充气,然后返回一个具有不同比例的BigDecimal。



例如,值1.00返回为1.000000,我假设因为我们将BigDecimals映射到定义为 NUMERIC(19,6)的列。我不能将列定义为所需的比例,因为我需要将美元和日元值(例如)存储在同一列中。为了外部报表工具的利益,我们需要将BigDecimals表示为数据库中的数字类型。



是否存在一个Hibernate UserType,它能正确映射BigDecimal,还是必须自己编写?



我通过使用调试器跨过Hibernate源代码找到了这个结果,同时试图找到我自己的问题


In Java, new BigDecimal("1.0") != new BigDecimal("1.00") i.e., scale matters.

This is apparently not true for Hibernate/SQL Server, however. If I set the scale on a BigDecimal to a particular value, save the BigDecimal to the database via Hibernate and then re-inflate my object, I get back a BigDecimal with a different scale.

For instance, a value of 1.00 is coming back as 1.000000, I assume because we're mapping BigDecimals to a column defined as NUMERIC(19,6). I can't just define the column as the required scale as I need to store both Dollar and Yen values (for example) in the same column. We need to represent the BigDecimals as numeric types in the database for the benefit of external reporting tools.

Does there exist a Hibernate UserType which maps BigDecimal "properly", or do I have to write my own?

解决方案

Just for informational sake, I can tell you that the creation of the BigDecimal coming back from the database is done by the proprietary JDBC driver's implementation of the 'getBigDecimal' method of the database-specific 'ResultSet' sub-class.

I found this out by stepping thru the Hibernate source code with a debugger, while trying to find the answer to my own question.

这篇关于如何在Hibernate中映射BigDecimal,以便恢复与我放入的相同比例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆