我正在使用Hibernate envers 5.2.10.Final版本来审核实体上的任何更改。在实体类中,也存在BigDecimal数据类型字段(因为需要保持精确的精度,所以不能像double那样更改为另一种数据类型)。
问题是,即使在没有值更改的情况下,BigDecimal值也会被跟踪为在审计表中被修改。
假设0(旧值)和0.00(新值)被跟踪为已修改,这是可以接受的,但在剥离零之后,也意味着新值也是0,这也是hibernate envers修改后的跟踪。
我厌倦了下面的几个选择,但没有用
@Audited(withModifiedFlag = true)
@Entity
class SomeEntity {
private Long id;
private BigDecimal value;
//getters and setters
}并且说有一个类可以设置值
class SomeClass{
Public void method(Foo foo) {
...//First approach
SomeEntity someEntity=newSomeEntity();
someEntity.setId(foo.getId());
someEntity.setValue(foo.getValue().stripTrailingZeros()); //still BigDecimal is tracked as modified in audit table
//Second approach
DecimalFormat decimalFormat.format("0.##");
SomeEntity someEntity=newSomeEntity();
someEntity.setId(foo.getId());
someEntity.setValue(new BigDecimal(decimalFormat(foo.getValue()))); //still BigDecimal is tracked as modified in audit table
//save to db
}
}任何帮助都是非常感谢的。
发布于 2022-08-01 11:16:37
这是因为org.hibernate.type.descriptor.java.BigDecimalTypeDescriptor#areEqual认为如果compareTo返回0,两个BigDecimal对象是相等的,这是0和0.00的情况。因此,如果要将这种值更改视为真正的值更改,则必须重写JavaTypeDescriptor for BigDecimal,并将org.hibernate.type.BigDecimalType替换为使用新JavaTypeDescriptor的新版本。
https://stackoverflow.com/questions/73165036
复制相似问题