I am getting below error while using lombok
and even it doesn't allow me to set id and version while creating student
instance.
Multiple markers at this line
- overrides com.example.demo.IModel.canEqual
- Generating equals/hashCode implementation but without a call to superclass, even though this class does not extend java.lang.Object. If this is
intentional, add '@EqualsAndHashCode(callSuper=false)' to your type.
- overrides com.example.demo.IModel.hashCode
- overrides com.example.demo.IModel.toString
- overrides com.example.demo.IModel.equals
IModel
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class IModel {
private String id;
private String version;
}
Student
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Student extends IModel{
private String firstName;
private String lastName;
}
In the main method, it doesn't allow me to set the value of Id and version field
Student s = Student.builder().firstName("Adam").lastName("Kerr").build();
Edit-1 @sfiss - As suggested, now I changed like below, but now I am not able to set firstName and lastName, only cab set id and version
Student.java
@Data
@Builder(builderMethodName = "studentBuilder")
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class Student extends IModel {
@NotEmpty(message = "{email.notempty}")
@Email
private String firstName;
private String lastName;
public Student(final String firstName, final String lastName, final String id, final String version) {
super(id, version);
this.firstName = firstName;
this.lastName = lastName;
}
}
IModel.java
@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
public class IModel {
private String id;
private String version;
}
There are multiple problems here, all of them relating to using lombok with inheritance:
Generating equals/hashCode implementation but without a call to superclass, even though this class does not extend java.lang.Object. If this is intentional, add '@EqualsAndHashCode(callSuper=false)' to your type.
The warning is given by @Data
because that usually generates equals/hashcode without the call to super. Fix it by adding @EqualsAndHashCode(callSuper = true)
.
The @Builder
gives you a compile warning because it will generate two static methods with the same name in both the super- and the subclass. Fix it by defining @Builder(builderMethodName = "studentBuilder")
on Student
.
You won't be able to set superclass properties on you studentBuilder because your superclass and subclass have a default constructor. Fix it by creating a constructor and moving the @Builder
annotation to it (ie annotate the constructor with @Builder
, not the class ):
Code:
@Builder(builderMethodName = "studentBuilder")
public Student(
final String firstName,
final String lastName,
final String id,
final String version) {
super(id, version);
this.firstName = firstName;
this.lastName = lastName;
}
Call your builder with the correct method ( IModel.builder()
vs Student.studentBuilder()
):
Student.studentBuilder().firstName("Name").build();
I also want to add some improvements to the above solution. While I like lombok as a tool (I really don't need to read that much boilerplate), the first solution to preventing boilerplate is to think whether you need all those getters and setters and ask yourself these questions:
Do you want bags of data? It is fine for some use cases, in others you want objects more in the sense of OOP, ie don't expose your state but behavior.
Do you really need mutability? If not, prefer @Value
.
Do you really need both constructor types (especially the no-args-constructor)? They are part of the problem here. Sometimes you need them for frameworks (proxies, reflection, ...) to work properly.
More specific to your code: You prefixed the superclass with "I" but it is not an interface. If it is meant as an abstract class, declare it abstract
and don't give it a @Builder
.
You can use @sfiss solution
or
You can use @Getter
and @Setter
annotations instead of @Data
annotation.
I had the same problem and i resolved it in this way hope it helps you. That help you also with abstract classes
Student.java
@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class Student extends IModel {
@NotEmpty(message = "{email.notempty}")
@Email
private String firstName;
private String lastName;
@Builder
public Student(final String firstName, final String lastName, final String id, final String version) {
super(id, version);
this.firstName = firstName;
this.lastName = lastName;
}
}
IModel.java
@Data
@NoArgsConstructor
@AllArgsConstructor
public class IModel {
private String id;
private String version;
}
When I built my Spring boot project, I got 20 warnings about a same thing. The warning shows: Generating equals/hashCode implementation but without a call to superclass
This warning is from lombook, it happens when we inherit a child class from parent class by using @Data @ToString @EqualsAndHashCode, IDE will trigger the warning: Generating equals/hashCode implementation but without a call to superclass
.
There are two solutions:
@EqualsAndHashCode(callSuper = true)
on the classsrc/main/java
. Note: this solution requires the version of lombook > 1.14.I recommend the solution 2, since you will not need to add the annotation to all the required classes.
To impletement the solution, you need to create lombook.config
in the path of src/main/java
. If you have more than one packages, you may need to create multiple config files.
The content of the config file includes:
config.stopBubbling=true
lombok.equalsAndHashCode.callSuper=call
When we rebuild our project, you will not get these warnings anymore.
Cheers!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.