How should equals() and hashCode() be implemented for JPA/Hibernate entities?
Reviewed by Gurusankar M.
β‘ Short Answer
Don't use the auto-generated DB id (it's null before persist) or all mutable fields. Use a stable business key, or a UUID assigned at construction. Inconsistent equals/hashCode breaks Sets and causes duplicates after merge/persist.
βCoffee Chat Question
Concept Made Simple
βHow should equals() and hashCode() be implemented for JPA/Hibernate entities?β
π§ Mind Map Answer
Remember It Faster
The trap: a new entity has id == null. If equals/hashCode use id, the object's hash changes once persisted β so it's lost in any HashSet it was added to before saving.
β¨οΈHands-on Keyboard
Learn by Doing
@Entity class Customer {
@Id private final UUID id = UUID.randomUUID(); // assigned at creation
@Override public boolean equals(Object o) {
return o instanceof Customer c && id.equals(c.id);
}
@Override public int hashCode() { return id.hashCode(); }
}π₯What If?
Think Beyond the Expected
Why can equals fail on a lazily-loaded entity inside a Set?
Hibernate may hand you a proxy subclass, so getClass()-based equals returns false against the real class. Use instanceof (not getClass()) and compare the id field directly β proxies still resolve the id.
πReal World
Duplicate rows in a @OneToMany Set, or entities 'disappearing' from a HashSet after save, almost always trace to id-based equals/hashCode on entities whose id is generated by the database.
π―Interviewer's Expectation
Keywords they're listening for:
β οΈCommon Mistakes
- βequals/hashCode based on a DB-generated id
- βUsing all mutable fields in equals
- βgetClass() comparison failing against lazy proxies
β Best Practices
- βAssign a UUID/business key at construction for identity
- βUse instanceof in equals to tolerate proxies
- βKeep hashCode stable across the entity's lifecycle
πFollow-up Questions
- 1Why prefer app-assigned UUIDs over DB-generated ids for identity?
- 2How do Hibernate proxies affect equals/getClass?
- 3When is it OK to rely on default Object identity?
π§©Related Technologies
Continue Learning with AI
Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly β AI is your learning companion, not a shortcut.
Plain-language foundations
I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner. Topic: OOP (Core Java) Interview question: "How should equals() and hashCode() be implemented for JPA/Hibernate entities?" Please: 1. Explain the core idea in simple, plain language, using an everyday analogy. 2. Define any technical terms you use. 3. Walk through one small, concrete example. 4. Finish with a single sentence I can easily remember. Keep the tone friendly and assume I'm new to this topic.
Was this answer helpful?
β Featured Products
Support our platform by exploring our recommended products.
As an Amazon affiliate, purchases through these links may earn us a small commission β at no extra cost to you. It helps keep Full Stack Interview Guru free.