Krzysztof Kot
Krzysztof Kot

Reputation: 658

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

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)

Upvotes: 8

Views: 15980

Answers (1)

Indra Basak
Indra Basak

Reputation: 7394

@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 
    ...

}

Upvotes: 20

Related Questions