Spring Data
Spring Data eliminates the DAO boilerplate that previously required writing CRUD operations by hand. Through
JpaRepositoryand query-method derivation, you get database access with almost zero implementation code. The important things to master are the N+1 query problem, transaction propagation rules, and when to step outside Spring Data's conventions for complex queries.
What You'll Find Here
| Topic | Description |
|---|---|
| JPA vs Hibernate vs Spring Data | The layered model: JPA spec, Hibernate implementation, Spring Data Commons, Spring Data JPA — how they relate. |
| Hibernate Basics | SessionFactory vs Session thread safety, entity lifecycle states, dirty checking, first-level cache, OSIV, LazyInitializationException. |
| JPA Basics | @Entity, @Id, @GeneratedValue, relationships (@OneToMany, @ManyToOne), fetch types, lifecycle callbacks, auditing. |
| Spring Data Repositories | CrudRepository, JpaRepository; query method derivation; @Query JPQL/native; pagination; Specification API. |
| Transactions | @Transactional — propagation, isolation, readOnly, rollbackFor; self-invocation trap; TransactionTemplate. |
| N+1 Query Problem | EAGER vs. LAZY fetch; JOIN FETCH; @EntityGraph; @BatchSize; DTO projections as fix. |
| Projections | Closed interface, open interface, DTO record, nested, and dynamic projections. |
| Spring Data Caching | @Cacheable, @CacheEvict, @CachePut, Caffeine, Redis, per-cache TTL configuration. |
Demos
Hands-on step-by-step code walkthroughs for each topic:
| Demo | What It Shows |
|---|---|
| JPA Basics Demo | Entity definition, ManyToOne relationships, lifecycle callbacks, Spring Data auditing. |
| Spring Data Repositories Demo | Derived queries, @Query JPQL/native, pagination, Specification API for dynamic filters. |
| Transactions Demo | @Transactional rollback, self-invocation trap, readOnly pattern, REQUIRES_NEW audit logging. |
| N+1 Problem Demo | Reproducing N+1 with SQL logs, fixing with JOIN FETCH, @EntityGraph, and @BatchSize. |
| Projections Demo | Closed interface, nested, DTO record, and dynamic projections with SQL output comparison. |
| Spring Data Caching Demo | @Cacheable hit/miss, @CacheEvict on writes, @CachePut, Redis per-cache TTL. |
Learning Path
- JPA vs Hibernate vs Spring Data — read this first; understand the full stack before diving into individual APIs.
- Hibernate Basics —
SessionFactoryvsSessionthread safety, entity states, dirty checking; these underpin everything else. - JPA Basics — entity mapping and relationship annotations are the foundation; understand
LAZYvs.EAGER. - Spring Data Repositories — query method derivation (
findByEmailAndStatus) and@Queryare the two main patterns. - Transactions — the self-invocation trap (
this.method()bypasses the proxy) is the most common Spring bug. - N+1 Problem — this is the single most common JPA performance problem; understand it deeply.
- Caching —
@Cacheablereduces database load; understand cache eviction and time-to-live.
Related Domains
- Spring Framework — transaction management and AOP underpin
@Transactional. - Databases — SQL, connection pooling, and schema migration (Flyway/Liquibase) work alongside JPA.
- Spring Boot —
spring-boot-starter-data-jpaauto-configures the JPA stack.