简体   繁体   中英

Hibernate-envers - Big decimal field is not audited correctly

I am using Hibernate envers 5.2.10.Final version to audit any changes on entity. And in entity class, there is BigDecimal data type fields also exists (can't change to another data type like double because it needs to hold accurate precision)

Issue is that BigDecimal value is tracked as modified in audit table even when there is no change in value

Let's Say 0 (old value) and 0.00(new value) is tracked as modified and this is accepted but after stripping zeros means new value also 0 and this is also tracked as modified by hibernate envers.

I tired few options as below but no use

@Audited(withModifiedFlag = true)
@Entity
class SomeEntity {
private Long id;
private BigDecimal value;
//getters and setters
}

And Say there is some class that sets value

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
}
}

Any help is much appreciated.

This is because org.hibernate.type.descriptor.java.BigDecimalTypeDescriptor#areEqual considers two BigDecimal objects as equal if compareTo returns 0 , which is the case for 0 and 0.00 . So if you want to treat this kind of value change as a real value change, you will have to override the JavaTypeDescriptor for BigDecimal and also replace org.hibernate.type.BigDecimalType with a new version that uses that new JavaTypeDescriptor .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM