Search code examples
javalombok

Warning equals/hashCode on Lombok's @Data annotation with inheritance


I have a entity which inherits from other. On other hand, I'm using Lombok project to reduce boilerplate code, so I put @Data annotation. The annotation @Data with inheritance produces the next 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.

I have the following questions:

  • Is it advisable to add annotation @EqualsAndHashCode(callSuper = true) or @EqualsAndHashCode(callSuper = false)?
  • If it is not added, which one is it: callSuper=false or callSuper=true?

Solution

  • The default value is false. That is the one you get if you don't specify it and ignore the warning.

    Yes, it is recommended to add an @EqualsAndHashCode annotation on the @Data annotated classes that extend something else than Object. I cannot tell you if you need true or false, that depends on your class hierarchy, and will need to be examined on a case-by-case basis.

    However, for a project or package, you can configure in lombok.config to call the super methods if it is not a direct subclass of Object.

    lombok.equalsAndHashCode.callSuper = call
    

    See the configuration system documentation on how this works, and the @EqualsEndHashCode documentation for the supported configuration keys.

    Disclosure: I am a lombok developer.