The relationship between @EqualsAndHashCode() and @Data annotation

Reprinted source: https://blog.csdn.net/zhanlanmg/article/details/50392266

Text mentioned to the following points:
1. This will generate annotations equals(Object other)and hashCode()method.
2. It uses non-static and non-transient properties
by default . 3. excludeSome properties
can be excluded by parameters . 4. You can ofspecify which properties to use only by parameters.
5. By default, it only uses the properties defined in the class and does not call the methods of the parent class.
6. You can callSuper=truesolve the previous problem. Let the generated method call the method of the parent class.

Another: @Dataequivalent to @Getter @Setter @RequiredArgsConstructor @ToString @EqualsAndHashCodea collection of these 5 annotations.

Through official documents, can be learned, when @Datathe time annotation, you have a @EqualsAndHashCodecomment, it will exist in this class equals(Object other)and hashCode()method, and does not use the property of the parent class, which leads to possible problems.
For example, if there are multiple classes with the same partial attributes, define them in the parent class, and it happens that id (database primary key) is also in the parent class, then there will be some objects in the comparison, they are not equal, but because of lombok automatically generated equals(Object other)and hashCode()methods determined to be equal, thereby causing an error.

The method to fix this problem is simple:
1. @Getter @Setter @ToStringplace @Dataand custom equals(Object other)and hashCode()methods, such as some class need only determines whether the primary key id equal will suffice.
2. Or use @Dataand add @EqualsAndHashCode(callSuper=false)notes when using .

Further reference to the article: https://blog.csdn.net/qq_27093465/article/details/90056695

Guess you like

Origin blog.csdn.net/aiwaston/article/details/104722283