Spring Data JPA makes the first version of a data layer easy, and the production version hard. These guides cover the mistakes that show up in real Java and Kotlin code: entities with broken equals and hashCode, repository defaults nobody checked, and transactions that don’t behave the way the annotation suggests.
Read them in this order. Each one builds on the previous.
The reading order
- Spring Data JPA Best Practices: Entity Design Guide: how to design entities that survive production, from IDs to equals and hashCode.
- Spring Data JPA Repositories: Best Practices and Cheatsheet: JpaRepository vs CrudRepository, projections, queries and stored procedures.
- Spring Data JPA Transactions and Queries: EntityManager, TransactionTemplate, @Transactional, native queries and the Criteria API.
- Spring Data JPA: Batch Insert Performance Tuning: how batch_size and reWriteBatchedInserts made saveAll() 6x faster in my benchmark.
- Spring Data JPA with Kotlin: Entities, Repositories, and Pitfalls: the same rules in Kotlin, and the data class traps that come with it.
- Kotlin data class in Spring Boot 4: what the compiler generates, and where JPA entities, Jackson defaults and validation break.
Who this guide is for
It’s for Java and Kotlin developers who already have a Spring Boot service with a database and want it to behave in production the way it behaved in tests. You don’t need to read Hibernate internals first. Each guide starts from code you have probably written and shows where it breaks.
Questions this series answers
- How should a JPA entity implement equals and hashCode, and which ID strategy should it use? See the entity design guide.
- JpaRepository or CrudRepository, and when is a projection better than loading the entity? See the repositories guide.
- Where should @Transactional go, and when do you need EntityManager or TransactionTemplate instead? See the transactions guide.
- Why is saveAll() slow, and which settings turn on real batch inserts? See the batch insert guide.
- Can a Kotlin data class be a JPA entity? See the Kotlin guide.
Test yourself
Tasks 7 to 10 of Java Interview Questions for Experienced Developers: 10 Code Review Tasks hide the bugs from these guides in short snippets: self-invocation with REQUIRES_NEW, a @Data entity with IDENTITY, a stale bulk update and a @Modifying query without a transaction.
Keep the rules enforced
Best practices only work when the build checks them. Spring Boot Best Practices That Should Fail Your Build shows how to turn rules like these into tests that fail the build.
Found this useful? Follow me on social media to stay updated.