Lombok @Data on a JPA Entity Caused a Broken equals() and 18,000 Duplicate Rows

A nightly data import began producing duplicate customer records, with a staging table holding about 18,000 more rows than the source file. The deduplication step relied on a HashSet, which uses equals and hashCode, and the entity had both generated by Lombok's @Data annotation. Because those methods included the database-assigned primary key, a customer loaded from the database and the same customer parsed from the import file were treated as different objects. The fix was to write equals and hashCode manually using the stable natural key of source system and external ID, so identity no longer depends on the database row. The article also notes that the generated hashCode can change after persistence and that generated toString may trigger lazy-loading problems.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.


Discussion (0)
Log in to join the discussion and vote.
Log in