<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/equals-and-hashcode-implementation-considerations-lfs1odvk8" -->

---
title: Equals and hashcode implementation considerations
description: When implementing equals() and hashCode() for JPA entities, generating the primary key before object creation simplifies the implementation significantly. By...
canonical: https://daily.dev/posts/equals-and-hashcode-implementation-considerations-lfs1odvk8
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Equals and hashcode implementation considerations | daily.dev
og:description: When implementing equals() and hashCode() for JPA entities, generating the primary key before object creation simplifies the implementation significantly. By...
og:url: https://daily.dev/posts/equals-and-hashcode-implementation-considerations-lfs1odvk8
og:image: https://api.daily.dev/og/posts/lFs1oDVk8.png
og:image:alt: Equals and hashcode implementation considerations
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Equals and hashcode implementation considerations

**[Wim Deblauwe](https://daily.dev/sources/wim-deblauwe)** · 2 min read · 0 upvotes · 0 comments

## Summary

When implementing equals() and hashCode() for JPA entities, generating the primary key before object creation simplifies the implementation significantly. By requiring the id in the constructor and asserting it is non-null, you can safely base both equals() and hashCode() solely on the id field. This approach aligns with the domain-driven design principle that entities are defined by their identity, not their attributes — two objects with the same id represent the same entity regardless of other field values.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.wimdeblauwe.com/blog/2021/04/26/equals-and-hashcode-implementation-considerations/>

## Similar posts on daily.dev

- [How to Avoid Common Pitfalls With JPA and Kotlin](https://daily.dev/posts/how-to-avoid-common-pitfalls-with-jpa-and-kotlin-hgchpvpwb) · Kotlin · 1 upvotes · 0 comments
- [Spring Data JDBC Made Easy with IntelliJ IDEA](https://daily.dev/posts/spring-data-jdbc-made-easy-with-intellij-idea-kqz5h1shs) · JetBrains · 0 upvotes · 0 comments
- [The quest for reflection-free access \(Part 1\)](https://daily.dev/posts/the-quest-for-reflection-free-access-part-1--5nqbuilyc) · Hibernate Blog · 0 upvotes · 0 comments

---

Tags: [#java](https://daily.dev/tags/java), [#spring](https://daily.dev/tags/spring), [#domain-driven-design](https://daily.dev/tags/domain-driven-design)

[View this post on daily.dev](https://daily.dev/posts/equals-and-hashcode-implementation-considerations-lfs1odvk8)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Equals and hashcode implementation considerations","url":"https://daily.dev/posts/equals-and-hashcode-implementation-considerations-lfs1odvk8","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/equals-and-hashcode-implementation-considerations-lfs1odvk8"},"datePublished":"2026-03-16T08:14:53.682Z","dateModified":"2026-03-16T08:30:05.765Z","description":"When implementing equals() and hashCode() for JPA entities, generating the primary key before object creation simplifies the implementation significantly. By...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/66934226c0ef8dd0dd36f3d9d846ca63?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/66934226c0ef8dd0dd36f3d9d846ca63?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Wim Deblauwe","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Wim Deblauwe","logo":"https://media.daily.dev/image/upload/s--g1qrglrf--/c_limit,w_256/f_auto,q_auto/v1773648779/logos/wim-deblauwe?_a=BAMAMiiu0","url":"https://daily.dev/sources/wim-deblauwe"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/equals-and-hashcode-implementation-considerations-lfs1odvk8","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"java,spring,domain-driven-design","timeRequired":"PT2M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Wim Deblauwe","item":"https://daily.dev/sources/wim-deblauwe"},{"@type":"ListItem","position":3,"name":"Equals and hashcode implementation considerations"}]}
```

