DEV Community

Cover image for @Data on a JPA Entity Gave Us a False `equals()` and 18,000 Duplicate Rows
Kamen
Kamen

Posted on Originally published at kamenivanov.substack.com AI-assisted

@Data on a JPA Entity Gave Us a False `equals()` and 18,000 Duplicate Rows

The nightly import had been running for months without a complaint. Then a report came back with customers listed twice, and a count of the staging table showed roughly 18,000 rows more than the source file contained. The import code had a deduplication step, it was three lines long, and it looked impossible to get wrong.

Set<Customer> toSave = new HashSet<>(existingCustomers);
toSave.addAll(importedCustomers);
customerDao.saveAll(toSave);
Enter fullscreen mode Exit fullscreen mode

A HashSet deduplicates by equals and hashCode, and the entity had both, because it was annotated with Lombok's @Data. That is the part everyone trusted and nobody read.

@Data generates equals and hashCode from every field of the class, and on a JPA entity every field includes the primary key. A customer loaded from the database carried an id. The same customer parsed from the import file had not been persisted yet, so its id was null. Two objects describing the same person, with the same external reference and the same email, were not equal to each other, because one of them had an id and the other did not. The set kept both, saveAll inserted the second one, and the deduplication step had been quietly doing nothing for as long as the id had been part of the comparison.

Query latency vs table size

The same generated method has a second failure mode that is easier to hit and harder to see. Because the id participates in hashCode, an entity's hash changes the moment it is persisted and the database assigns a value. An object placed in a HashSet before the flush lands in one bucket, and after the flush the set looks for it in another. The object is in the collection and the collection says it is not there. Any code that holds entities in a set or uses them as map keys across a flush boundary is exposed to this, and it fails intermittently, which is how it survives review.

The fix was to stop deriving identity from the database. The import records had a natural key already, a source system and an external id, unique by contract and never changed after creation, so equals and hashCode were written by hand against those two fields and nothing else. Identity became a property of the record rather than of its row, which means it is the same before the insert, after the insert and after a detached object is merged back.

@Override
public boolean equals(Object o) {
    if (this == o) {
        return true;
    }
    if (!(o instanceof Customer other)) {
        return false;
    }
    return sourceSystem == other.sourceSystem && externalId.equals(other.externalId);
}

@Override
public int hashCode() {
    return Objects.hash(sourceSystem, externalId);
}
Enter fullscreen mode Exit fullscreen mode

FK index missing vs present

When there is no natural key to lean on, the usual alternative is a UUID assigned in the constructor rather than by the database, which gives the same stability without requiring the business to supply one.

There is a smaller reason to keep @Data away from entities, and it shows up in logs rather than in data. The annotation also generates toString across all fields, including lazily loaded associations, so printing an entity can trigger a query per association, or a LazyInitializationException outside a session. The usual compromise, if Lombok is going to stay on the class, is @Getter and @Setter rather than @Data, with equals, hashCode and toString written.

The broader point is that an entity has two identities, the row it maps to and the thing it represents, and they are not available at the same moments in its lifecycle. A generated method cannot know which one you meant. Ours picked the row, in a set that was asking about the thing.


More like this? I write short production war stories like this one, plus a deep-dive architecture series. Subscribe here if you want both in your inbox.


Recommended Resources for Java & Spring Boot Engineers

If you are preparing for Senior/Lead Java interviews or looking to solidify your Spring Boot & Architecture skills, check out these highly-rated resources from the Javarevisited publication (Use promo code friends20 for an exclusive 20% discount automatically applied at checkout):

Top comments (0)