Skip to main content

Spring Data

Spring Data eliminates the DAO boilerplate that previously required writing CRUD operations by hand. Through JpaRepository and 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

TopicDescription
JPA vs Hibernate vs Spring DataThe layered model: JPA spec, Hibernate implementation, Spring Data Commons, Spring Data JPA — how they relate.
Hibernate BasicsSessionFactory 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 RepositoriesCrudRepository, JpaRepository; query method derivation; @Query JPQL/native; pagination; Specification API.
Transactions@Transactional — propagation, isolation, readOnly, rollbackFor; self-invocation trap; TransactionTemplate.
N+1 Query ProblemEAGER vs. LAZY fetch; JOIN FETCH; @EntityGraph; @BatchSize; DTO projections as fix.
ProjectionsClosed 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:

DemoWhat It Shows
JPA Basics DemoEntity definition, ManyToOne relationships, lifecycle callbacks, Spring Data auditing.
Spring Data Repositories DemoDerived 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 DemoReproducing N+1 with SQL logs, fixing with JOIN FETCH, @EntityGraph, and @BatchSize.
Projections DemoClosed 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

  1. JPA vs Hibernate vs Spring Data — read this first; understand the full stack before diving into individual APIs.
  2. Hibernate BasicsSessionFactory vs Session thread safety, entity states, dirty checking; these underpin everything else.
  3. JPA Basics — entity mapping and relationship annotations are the foundation; understand LAZY vs. EAGER.
  4. Spring Data Repositories — query method derivation (findByEmailAndStatus) and @Query are the two main patterns.
  5. Transactions — the self-invocation trap (this.method() bypasses the proxy) is the most common Spring bug.
  6. N+1 Problem — this is the single most common JPA performance problem; understand it deeply.
  7. Caching@Cacheable reduces database load; understand cache eviction and time-to-live.
  • Spring Framework — transaction management and AOP underpin @Transactional.
  • Databases — SQL, connection pooling, and schema migration (Flyway/Liquibase) work alongside JPA.
  • Spring Bootspring-boot-starter-data-jpa auto-configures the JPA stack.