java JPA 自动 BigDecimal 转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11898855/
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-31 06:49:28  来源:igfitidea点击:

JPA automatic BigDecimal conversion

javahibernatejpadb2bigdecimal

提问by Udo Held

We are generating our jpa access layers with MyEclipse. Afterwards we have the generated models and data layer access services. We ran into some problems for some fields with a defined precision.

我们正在使用 MyEclipse 生成我们的 jpa 访问层。之后我们有了生成的模型和数据层访问服务。我们在某些具有定义精度的领域遇到了一些问题。

Entity:

实体:

@Entity
public class TestEntity{
   @Column(name="DECTEST", scale = 3, precision = 13)
   BigDecimal decTest;

}

Now we create a bean and try to save it:

现在我们创建一个 bean 并尝试保存它:

TestEntity te = new TestEntity();
te.setDecTest(new BigDecimal(1.2));

TestEntityService.save(te);

We get the following error: Caused by: com.ibm.db2.jcc.c.SqlException: [ibm][db2][jcc][t4][1037][11190] Exception occurred during BigDecimal conversion. See attached Throwable for details.

我们得到以下错误:Caused by: com.ibm.db2.jcc.c.SqlException: [ibm][db2][jcc][t4][1037][11190] BigDecimal 转换期间发生异常。有关详细信息,请参阅随附的 Throwable。

Caused by: com.ibm.db2.jcc.a.a: [ibm][db2][jcc][converters][608][10994] Overflow occurred during numeric data type conversion of "1.1999999999999999555910790149937383830547332763671875".
at com.ibm.db2.jcc.a.e.a(e.java:61)
at com.ibm.db2.jcc.b.jb.a(jb.java:1772)
... 73 more

The problem seems to be that our BigDecimals scale is higher then the one from the database.

问题似乎是我们的 BigDecimals 比例高于数据库中的比例。

A working workaround is:

一种解决方法是:

TestEntity te = new TestEntity();

BigDecimal decTest = new BigDecimal(1.2);

te.setDecTest(decTest.setScale(3,RoundingMode.HALF_UP);

TestEntityService.save(te);

With this workaround we reduce the BigDecimals precicsion manually to the one of the database.

通过这种变通方法,我们手动将 BigDecimals 精度降低到数据库之一。

However if the data model changes we would have to adjust the scale there manually. Is there a way to get our jpa / hibernate implementation to do that conversion for us automatically? E.g. with setting a property. Doing it at the spot where one is creating the bean would be the wrong spot to do it anyways.

但是,如果数据模型发生变化,我们将不得不手动调整比例。有没有办法让我们的 jpa / hibernate 实现自动为我们进行转换?例如设置属性。在创建 bean 的地方做这件事无论如何都是错误的。

回答by JB Nizet

You could use a custom user type, or you could simply implement the setter of the property like this:

您可以使用自定义用户类型,也可以简单地实现属性的 setter,如下所示:

public void setDecTest(BigDecimal decTest) {
    this.decTest = decTest.setScale(3, RoundingMode.HALF_UP));
}

This rounding would thus be encapsulated in the entity.

因此,这种舍入将被封装在实体中。

Note that using a double to initialize a BigDecimal is a bit strange. If you want 1.2to be stored in the BigDecimal, use new BigDecimal("1.2"), and you won't have a BigDecimal initialized with 1.1999999999999999555910790149937383830547332763671875.

请注意,使用 double 来初始化 BigDecimal 有点奇怪。如果您想1.2存储在 BigDecimal 中,请使用new BigDecimal("1.2"),并且您将不会使用1.1999999999999999555910790149937383830547332763671875.