145

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.

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?

Michael Lihs
  • 7,460
  • 17
  • 52
  • 85
Pau
  • 14,917
  • 14
  • 67
  • 94

4 Answers4

163

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.

Roel Spilker
  • 32,258
  • 10
  • 68
  • 58
  • Worked for me. But just keep in mind that for the delombok plugin to pick this config file up it should be placed in the java source root directory, not in the resources directory, i.e. in src/main/java and not in src/main/resources – Alok P Mar 21 '18 at 13:19
  • 3
    @Roel I'm wondering why the default is false. I would've expected the opposite. Also, is there an equivalent way to get toString() to call super by default? I see I can do "@ToString(callSuper=true)", but not seeing any such configuration setting. Thanks. – David Siegal Apr 16 '18 at 20:19
  • 1
    Does it matter if I add @EqualsAndHashCode(callSuper=true) before or after @Data ? – Anna Klein Aug 07 '18 at 14:37
  • 1
    @AnnaKlein the order doesn't matter – dan carter Sep 15 '19 at 22:33
66

@EqualsAndHashCode(callSuper=true) should resolve the warning.

noscreenname
  • 3,314
  • 22
  • 30
  • 2
    This should be the accpeted answer since I don't think the suggestion of Roel should be done "lombok.equalsAndHashCode.callSuper = call" instead a decision for each class should be made. – Anna Klein Aug 07 '18 at 14:35
  • 9
    @AnnaKlein I don't think so. In fact this answer should be a comment, there's no new information here, you can find that in my question. I did know `@EqualsAndHashCode` resolves the warning. – Pau Sep 07 '18 at 14:40
  • Actually per the accepted answer (and my answer below) you should choose between 'callSuper=true' or 'callSuper=false' in the annotation. – Adam Wise Apr 15 '19 at 15:25
49

The main original question is:

Is it advisable to add annotation @EqualsAndHashCode (callSuper = true) or @EqualsAndHashCode (callSuper = false)?

The accepted answer is basically just:

...that depends...

To expand on that, the documentation on @EqualsAndHashCode has some solid guidance on which to choose. Especially this, IMHO:

By setting callSuper to true, you can include the equals and hashCode methods of your superclass in the generated methods. For hashCode, the result of super.hashCode() is included in the hash algorithm, and forequals, the generated method will return false if the super implementation thinks it is not equal to the passed in object. Be aware that not all equals implementations handle this situation properly. However, lombok-generated equals implementations do handle this situation properly, so you can safely call your superclass equals if it, too, has a lombok-generated equals method.

To distill this down a bit: Chose 'callSuper=true' if you are inheriting from a superclass that either has no state information, or itself is using the @Data annotation, or has implementations of equals/hash that "handle the situation properly" - which I interpret to mean returning a proper hash of the state values.

Adam Wise
  • 2,043
  • 20
  • 17
28

If you want to compare the members of the superclass as well, then use @EqualsAndHashCode(callSuper=true). If, however, you only want to compare fields in the current class you can use @EqualsAndHashCode(callSuper=false) which is the default option.

If you use the Delombok-feature you can see that the difference is that when set to true this line is added to the generated equals method if (!super.equals(o)) return false;. If you have members in the superclass that should be taken into account when comparing two objects, then it has to be set to true to compare correctly.

EvR2f
  • 431
  • 4
  • 7