Scene:
The price data type in the database is Decimal, and the price data type in entity class A is BigDecimal. When it is necessary to use equals method to compare the A1 object passed from the front end with the A2 object quashed from the database, the result is false even if the object attributes are the same.
why
Debug found that the price value of the object A1 passed from the front end was 0.1 after JSON transformation, while the price value of the a2 object query from the database was 0.1000. Alt + INSERT equals method was used to call the EQUALS method of BigDecimal.
BigDecimal a = new BigDecimal("1.0");
BigDecimal b = new BigDecimal("1.00");
System.out.println(a.compareTo(b) == 0 ); // true
System.out.println(a.equals(b)); // false
The equals method of BigDecimal compares precision and values, while the compareTo method only compares values.
The solution
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ProProductSkuEntity that = (ProProductSkuEntity) o;
return Objects.equals(id, that.id) &&
Objects.equals(productId, that.productId) &&
compareBigDecimal(price, that.price);
}
public boolean compareBigDecimal(BigDecimal a, BigDecimal b) {
return (a == b) || (a != null && a.compareTo(b) == 0);
}
Modify the equals method overwritten in the entity class, customize the compareBigDecimal method, use compareTo to compare values of the BigDecimal type, and you can also put this method in the utility class.
Read More:
- Error in idea @ data entity class get / set
- Divide in [JavaEE] BigDecimal, divisor cannot be 0. java.lang.ArithmeticException : / by zero
- How to Solve Infinite scroll the pit encountered
- Vacuum box installation CentOS encountered the pit
- Using the pit in Git that rebase encountered two could not apply XXXX
- Step on the pit — error reported by sqoop tool.ExportTool : Error during export
- Data analysis to obtain Yahoo stock data: some problems are encountered when using panda datareader (cannot import name ‘is_ list_ Like ‘problem)
- Problems encountered after Maven installation or upgrade: could not find or load main class org.codehaus.plexus .class…..
- Error c2011: “a certain class”: redefinition of “class” type
- Java project cannot import entity class package and service package, controller layer reports error, fix project setup is OK
- No data: data: get host by name failed in TCP_ Connect() error resolution
- Java gets the type t.class of generic t
- Some problems encountered by Android Aidl
- Errors encountered by elasticsearch in creating index and mapping
- C++ Error: allocating an object of abstract class type
- In thinkphp5, we encountered the problem of class’ phpoffice / phpspredsheet / spreadsheet ‘not found
- Summary of problems encountered by CodeBlocks + OpenGL
- Error: requesting member ‘*’ of ‘*’, which is a non-class type ‘*’
- Problems encountered by Android Studio: Session’app’: Error Installing APKs
- Error converting data type nvarchar to datetime