Entity与SuperEntity未加注解@EqualsAndHashCode(callSuper = true)导致子类继承后即便加上该注解hashCode也不一致,调用.equals将返回false
pinkcao opened this issue · 2 comments
pinkcao commented
版本信息:
JDK 版本(必填) : 11
源码版本(必填):4.12.2 datasource-column
MySQL(必填): 5.7.9
Nacos(必填):2.0.1
问题描述:
在用于比较两个继承了Entity的相同类的对象时调用.equals返回false
Entity与SuperEntity未加注解@EqualsAndHashCode(callSuper = true)导致子类继承后即便加上该注解hashCode也不一致,调用.equals将返回false
是否考虑在Entity类上加上注解
@EqualsAndHashCode(callSuper = true)
在SuperEntity类上加上注解
@EqualsAndHashCode
报错截图
重现步骤
1.新建一个TestExtend类
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
public class TestExtend {
private String crazy;
}
2.新建一个Test类继承TestExtend,并加上注解@EqualsAndHashCode(callSuper = true)
@EqualsAndHashCode(callSuper = true)
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Accessors(chain = true)
public class Test extends TestExtend {
private String prop1;
private String prop2;
}
3.调用.equals方法
Test test1 = new Test();
test1.setProp1("a").setProp2("b").setCrazy("c");
Test test2 = new Test();
test2.setProp1("a").setProp2("b").setCrazy("c");
test1.equals(test2); // return false,实际上应该return true
4.在被继承的类TextExtend上增加注解@EqualsAndHashCode
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
public class TestExtend {
private String crazy;
}
5.再次调用.equals
Test test1 = new Test();
test1.setProp1("a").setProp2("b").setCrazy("c");
Test test2 = new Test();
test2.setProp1("a").setProp2("b").setCrazy("c");
test1.equals(test2); // return true
zuihou commented
已提交