本文介绍了BigDecimal精度不会与JPA批注保持一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用javax.persistence API和Hibernate在Oracle 11g Express数据库中创建注释和持久实体及其属性。



我有以下属性一个实体:

  @Column(precision = 12,scale = 9)
private BigDecimal weightedScore;

目标是坚持一个十进制值,最多12位数,最多9位

在计算 weightedScore 后,结果为0.1234,但是一旦我提交具有Oracle数据库的实体的值显示为0.12。



我可以通过使用EntityManager对象查询条目或直接在Web浏览器的Oracle Application Express(Apex)接口中查看它。 p>

我应该如何注释我的BigDecimal属性,以便正确保持精度?



注意:内存HSQL数据库来运行我们的单元测试,它不会遇到缺少精度的问题,无论是否有 @Column 注释。



更新:



查看表格描述, weightedScore 列的定义是 NUMBER(19,2)。我现在也尝试将注释更改为 @Column(columnDefinition =Number(12,9)),但这不起作用。有谁知道为什么Oracle对这些注释没有反应吗?

我找到了答案。 Huzzah!

我尝试通过Oracle Apex界面执行以下查询:

alter table NODE modify(WEIGHTEDSCORE NUMBER(12,9));



我收到一个错误,指出包含数据的列不能被修改精度较低或规模较小。这是我的问题!



因为我试图用现有数据改变表格,所以我需要删除表格并重新初始化它,或者将列更改为仅具有更高的精确度和规模。



我尝试了以下查询并获得成功:

alter table NODE modify(WEIGHTEDSCORE NUMBER(26,9));



原因是我想为小数点右侧,所以我在总体精度上加上7来补偿规模的增加。这样,列可以保留小数点左侧的所有现有精度,同时在右侧添加精度。


I am using the javax.persistence API and Hibernate to create annotations and persist entities and their attributes in an Oracle 11g Express database.

I have the following attribute in an entity:

@Column(precision = 12, scale = 9)
private BigDecimal weightedScore;

The goal is to persist a decimal value with a maximum of 12 digits and a maximum of 9 of those digits to the right of the decimal place.

After calculating weightedScore, the result is 0.1234, but once I commit the entity with the Oracle database, the value displays as 0.12.

I can see this by either using an EntityManager object to query the entry or by viewing it directly in the Oracle Application Express (Apex) interface in a web browser.

How should I annotate my BigDecimal attribute so that the precision is persisted correctly?

Note: We use an in-memory HSQL database to run our unit tests, and it does not experience the issue with the lack of precision, with or without the @Column annotation.

Update:

Looking at the table description, the definition of the weightedScore column is NUMBER(19, 2). I have now also tried changing the annotation to @Column(columnDefinition="Number(12, 9)"), but this has had no effect. Does anyone know why Oracle is unresponsive to these annotations?

解决方案

I found the answer. Huzzah!

I attempted to execute the following query through the Oracle Apex interface:

alter table NODE modify (WEIGHTEDSCORE NUMBER(12, 9));

I received an error stating that a column containing data cannot be modified to have less precision or less scale. This was my issue!

Because I was attempting to alter table with existing data, I needed to either drop the table and re-initialize it, or alter the column to only have more precision and scale.

I attempted the following query with success:

alter table NODE modify (WEIGHTEDSCORE NUMBER(26, 9));

The reasoning is that I want to add 7 places of precision to the right of the decimal, so I am adding 7 to the overall precision to compensate for the increase in scale. That way, the column can keep all existing precision on the left of the decimal while adding precision on the right side.

这篇关于BigDecimal精度不会与JPA批注保持一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 03:05
查看更多