Warning equals/hashCode on @Data annotation lombok with inheritance

后端 未结 4 451
轻奢々
轻奢々 2020-12-23 03:01

I have a entity which inherits from other. On other hand, I\'m using lombok project to reduce boilerplate code, so I put @Data annotation. The annotation

4条回答
  •  暖寄归人
    2020-12-23 03:18

    If you want to compare the members of the superclass as well, then use @EqualsAndHashCode(callSuper=true). If, however, you only want to compare fields in the current class you can use @EqualsAndHashCode(callSuper=false) which is the default option.

    If you use the Delombok-feature you can see that the difference is that when set to true this line is added to the generated equals method if (!super.equals(o)) return false;. If you have members in the superclass that should be taken into account when comparing two objects, then it has to be set to true to compare correctly.

提交回复
热议问题