| name | spring-boot-data-ddd |
| description | Spring Boot 4 data layer implementation for Domain-Driven Design. Use when implementing JPA or JDBC aggregates, Spring Data repositories, transactional services, projections, or entity auditing. Covers aggregate roots with AbstractAggregateRoot, value object mapping, EntityGraph for N+1 prevention, and Spring Boot 4 specifics (JSpecify null-safety, AOT repositories). For DDD concepts and design decisions, see the domain-driven-design skill. |
Spring Boot Data Layer for DDD
Implements DDD tactical patterns with Spring Data JPA and Spring Data JDBC in Spring Boot 4.
Technology Selection
| Choose | When |
|---|---|
| Spring Data JPA | Complex queries, existing Hibernate expertise, need lazy loading |
| Spring Data JDBC | DDD-first design, simpler mapping, aggregate-per-table, no lazy loading |
Spring Data JDBC enforces aggregate boundaries naturally—recommended for new DDD projects.
Core Workflow
- Define aggregate root → Extend
AbstractAggregateRoot<T>for domain events - Map value objects → Use
@Embeddedor@Converterfor immutability - Create repository interface → One per aggregate root, extend appropriate base
- Implement service layer →
@Transactionalon public methods, one aggregate per transaction - Add projections → Interface or record-based for read operations
Quick Patterns
See EXAMPLES.md for complete working examples including:
- Aggregate Root with AbstractAggregateRoot and domain events (Java + Kotlin)
- Repository with EntityGraph for N+1 prevention
- Transactional Service with proper boundaries
- Value Objects (Strongly-typed IDs, Money pattern)
- Projections for efficient read operations
- Auditing with automatic timestamps
Spring Boot 4 Specifics
- JSpecify null-safety:
@NullMarkedand@Nullableannotations - AOT Repository Compilation: Enabled by default for faster startup
- Jakarta EE 11: All imports use
jakarta.*namespace
Detailed References
- Examples: See EXAMPLES.md for complete working code examples
- Troubleshooting: See TROUBLESHOOTING.md for common issues and Boot 4 migration
- Aggregates & Entities: See references/aggregates.md for complete patterns with value objects, typed IDs, auditing
- Repositories & Queries: See references/repositories.md for custom queries, projections, specifications
- Transactions: See references/transactions.md for propagation, isolation, cross-aggregate consistency
Anti-Pattern Checklist
| Anti-Pattern | Fix |
|---|---|
FetchType.EAGER on associations |
Use LAZY + @EntityGraph when needed |
| Returning entities from controllers | Convert to DTOs in service layer |
@Transactional on private methods |
Use public methods (proxy limitation) |
Missing readOnly = true on queries |
Add for read operations (performance) |
| Direct aggregate-to-aggregate references | Reference by ID only |
| Multiple aggregates in one transaction | Use domain events for eventual consistency |
Critical Reminders
- One aggregate per transaction — Cross-aggregate changes via domain events
- Repository per aggregate root — Never for child entities
- Value objects are immutable — No setters, return new instances
- Flush before events — Call
repository.save()before events dispatch - Test with
@DataJpaTest— UseTestEntityManagerfor setup