Home » Java » Lombok – warning with @Data when equals and hashcode are implemented

Lombok – warning with @Data when equals and hashcode are implemented

Posted by: admin December 28, 2021 Leave a comment

Questions:

I have JPA entity that extends other abstract class. I want to use @Data to avoid writing setters and getters but my equals and hashcode methods exists.

I get warning but I think I should not:

server\entity\User.java:20: warning: 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.
@Data
^

In my User class:

@Data
@Entity
public class User extends AbstractAuditingEntity implements Serializable {

    ....

    @Override
    public boolean equals(Object o) {
       ...
    }

    @Override
    public int hashCode() {
       ...
    }
}

When I add additionally @EqualsAndHashCode(callSuper = false) to @Data I get:

server\entity\User.java:21: warning: Not generating equals and hashCode: A method with one of those names already exists. (Either both or none of these methods will be generated).
@EqualsAndHashCode(callSuper = false)
Answers:

@Data is a shortcut for @ToString, @EqualsAndHashCode, @Getter, @Setter, and @RequiredArgsConstructor. Since you just want @Getterand @Setter, why don’t you just use them (this will avoid your exception or warning messages),

@Entity
@Getter
@Setter
public class User extends AbstractAuditingEntity implements Serializable 
    ...

}