简体   繁体   中英

Does Lombok @Data override the existing toString and hashCode methods?

Recently we started make use of Lombok features in our project. we have @Data annotation for the Domain object, due to this running with some exception thrown by hashCode() method provided by Lombok api. Later, when I added @Setter and @Getter instead of @Data , I didn't see any issues.

Question1: Does Lombok @Data override the existing methods in a class like hashCode() and toString() ?

Question2: why is hashCode() making problems here?

Yes, @Data implies @EqualsAndHashCode and @ToString . See the @Data documentation .

The generated hashCode() method will call the hashCode methods for all fields. So if one of the values of the fields throws an exception, so will this.

One other scenario is that you have circular object references: If one object has a field that contains an object that has a field that refers to the first object, invoking the hashCode method will trigger a StackOverflow.

Disclosure: I am one of the Lombok developers.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM