java 如何在 Hibernate 中映射 BigDecimal 以便返回与输入相同的比例?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6544232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 16:18:01  来源:igfitidea点击:

How do I map a BigDecimal in Hibernate so I get back the same scale I put in?

javasql-serverhibernatehibernate-mappingbigdecimal

提问by Robert Atkins

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

在 Java 中,new BigDecimal("1.0") != new BigDecimal("1.00")规模很重要

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.

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

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.

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

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

是否存在“正确”映射 BigDecimal 的 Hibernate UserType,还是我必须自己编写?

回答by DuncanKinnear

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.

只是为了提供信息,我可以告诉您,从数据库返回的 BigDecimal 的创建是通过专有 JDBC 驱动程序实现特定于数据库的“ResultSet”子类的“getBigDecimal”方法完成的。

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 源代码发现了这一点,同时试图找到我自己问题的答案。

回答by Tapas Bose

I think this will work, I didn't test it though.

我认为这会奏效,但我没有测试过。

public class BigDecimalPOJO implements Serializable {

    private static final long serialVersionUID = 8172432157992700183L;

    private final int SCALE = 20;
    private final RoundingMode ROUNDING_MODE = RoundingMode.CEILING;
    private BigDecimal number;

    public BigDecimalPOJO() {

    }

    public BigDecimal getNumber() {
        return number.setScale(SCALE, ROUNDING_MODE);
    }

    public void setNumber(BigDecimal number) {
        this.number = number.setScale(SCALE, ROUNDING_MODE);
    }
}

回答by Distortum

Not sure, but you can check equality using a.compareTo(b) == 0.

不确定,但您可以使用a.compareTo(b) == 0.