dromara / lamp-cloud

lamp-cloud 支持jdk21、jdk17、jdk11、jdk8,ta基于 SpringCloud + SpringBoot 开发的微服务中后台快速开发平台,专注于多租户(SaaS架构)解决方案,亦可作为普通项目(非SaaS架构)的基础开发框架使用,目前已实现插拔式数据库隔离、SCHEMA隔离、字段隔离 等租户隔离方案。
http://tangyh.top
Apache License 2.0
5.47k stars 1.67k forks source link

Entity与SuperEntity未加注解@EqualsAndHashCode(callSuper = true)导致子类继承后即便加上该注解hashCode也不一致,调用.equals将返回false #211

Closed pinkcao closed 1 year ago

pinkcao commented 1 year ago

版本信息:

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
pinkcao commented 1 year ago

可参考https://blog.csdn.net/qq1805696978/article/details/106288958

zuihou commented 1 year ago

已提交