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. exclude
Some properties
can be excluded by parameters . 4. You can of
specify 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=true
solve the previous problem. Let the generated method call the method of the parent class.
Another: @Data
equivalent to @Getter @Setter @RequiredArgsConstructor @ToString @EqualsAndHashCode
a collection of these 5 annotations.
Through official documents, can be learned, when @Data
the time annotation, you have a @EqualsAndHashCode
comment, 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 @ToString
place @Data
and 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 @Data
and add @EqualsAndHashCode(callSuper=false)
notes when using .
Further reference to the article: https://blog.csdn.net/qq_27093465/article/details/90056695