Spring Boot, JPA & Database Mastery - Complete Production Guide
A comprehensive, production-grade guide to mastering Spring Boot project structure, all JPA relationship types, database modelling, ID strategies, and real-world production patterns. Designed for developers and architects at every level.
Series Navigation
| Part | Title | Key Topics |
|---|---|---|
| Part 1 | Spring Boot Project Structure | Maven layout, package strategies, pom.xml, profiles, auto-configuration, multi-module |
| Part 2 | Database Modelling and ID Strategies | Entity design, ID generation, composite keys, normalization, indexes, Flyway |
| Part 3 | Relationships: One-To-One and One-To-Many | @OneToOne, @OneToMany, cascade, fetch types, N+1 problem, solutions |
| Part 4 | Relationships: Many-To-One and Many-To-Many | @ManyToOne, @ManyToMany, custom join entity, projections, N+1 at scale |
| Part 5 | Production Patterns and Configurations | Transactions, locking, auditing, soft deletes, HikariCP, AWS RDS, Flyway, testing |
| Part 6 | Interview Questions and Architect Guide | 60+ questions, follow-ups, tricky edge cases, architect-level discussions |
Domain Model Used Throughout This Guide
All code examples across every part use the same realistic e-commerce domain to build a mental model that connects across all concepts.
+------------------+ +-------------------+
| User |-------->| UserProfile | OneToOne
+------------------+ +-------------------+
|
| OneToMany
v
+------------------+ +-------------------+
| Order |-------->| Payment | OneToOne
+------------------+ +-------------------+
|
| OneToMany
v
+------------------+ +-------------------+
| OrderItem |-------->| Product | ManyToOne
+------------------+ +-------------------+
|
| ManyToMany
v
+-------------------+
| Category |
+-------------------+
+------------------+ +-------------------+
| User |<------->| Role | ManyToMany
+------------------+ +-------------------+
Entities at a Glance
| Entity | Relationships | Purpose |
|---|---|---|
| User | OneToOne -> UserProfile, OneToMany -> Order, ManyToMany -> Role | Customer or admin |
| UserProfile | OneToOne <- User | Extended user data, bio, avatar |
| Role | ManyToMany <- User | ADMIN, CUSTOMER, SUPPORT |
| Address | ManyToOne -> User | Shipping and billing addresses |
| Product | ManyToMany -> Category, OneToMany <- OrderItem | Sellable items |
| Category | ManyToMany <- Product | Product classification |
| Order | ManyToOne -> User, OneToMany -> OrderItem, OneToOne -> Payment | Customer purchase |
| OrderItem | ManyToOne -> Order, ManyToOne -> Product | Line items in an order |
| Payment | OneToOne -> Order | Payment record |
Technology Stack
| Technology | Version | Purpose |
|---|---|---|
| Java | 17 LTS | Programming language |
| Spring Boot | 3.2.x | Application framework |
| Spring Data JPA | 3.2.x | Repository abstraction |
| Hibernate | 6.x | JPA implementation (ORM) |
| MySQL | 8.0+ | Relational database |
| AWS RDS | MySQL 8.0 | Managed cloud database |
| HikariCP | 5.x | JDBC connection pooling |
| Flyway | 10.x | Database migration tool |
| Maven | 3.8+ | Build and dependency management |
| Lombok | 1.18+ | Reduce boilerplate code |
| MapStruct | 1.5+ | DTO to entity mapping |
| TestContainers | 1.19+ | Integration testing with real MySQL |
The Stack - How Everything Connects
Your Application Code (Controllers, Services)
|
Spring Data JPA (JpaRepository, @Query, Specifications)
|
JPA Specification (EntityManager, JPQL, Criteria API)
|
Hibernate (JPA Implementation - ORM, Session, Cache)
|
HikariCP (Connection Pool - manages DB connections)
|
JDBC (Java Database Connectivity - java.sql.*)
|
MySQL Connector/J (MySQL JDBC Driver)
|
MySQL 8.0 on AWS RDS (The actual database)
Understanding this stack is critical. When a query fails or performs poorly, knowing which layer the problem originates from saves hours of debugging.
Prerequisites
- Java: OOP fundamentals, generics, annotations, lambda expressions
- SQL: SELECT, JOIN, WHERE, GROUP BY, PRIMARY KEY, FOREIGN KEY, INDEX
- Spring Basics: Dependency Injection, IoC container, Spring beans
- Maven: Basic understanding of pom.xml and dependency management
Quick Reference - Golden Rules
These are the rules that matter most in production. Every rule is explained in detail in the relevant part.
Always Do This
- Use
LAZYfetch type everywhere by default - Use constructor injection, never field injection
- Have a dedicated DTO layer - never expose entities in your REST API
- Use
spring.jpa.open-in-view=false - Use Flyway for all schema changes
- Always implement
equals()andhashCode()in entities used in Sets - Use
Setinstead ofListfor@ManyToMany - Add helper methods to maintain bidirectional relationship consistency
- Use
@Transactional(readOnly = true)for all read operations - Use optimistic locking (
@Version) for concurrent writes
Never Do This
- Never use
spring.jpa.hibernate.ddl-auto=updatein production - Never use
CascadeType.ALLon@ManyToManywithout understanding implications - Never use
FetchType.EAGERunless you have a very specific reason - Never expose
@Transactionalmethods inside the same class (self-invocation breaks proxy) - Never use
@OneToManywithout the corresponding@ManyToOneon the owning side - Never use unidirectional
@OneToMany(causes an extra join table in MySQL) - Never use
FetchType.EAGERon more than one collection per entity (Cartesian product) - Never catch generic
Exceptioninside a@Transactionalmethod and swallow it
How to Use This Guide
- Beginners: Read Part 1 and Part 2 first to understand fundamentals
- Developers with Spring experience: Jump to Part 3 and Part 4 for relationships
- Senior/Lead developers: Focus on Part 5 for production-hardening patterns
- Interview preparation: Read Part 6, but make sure you understand the concepts in Parts 1-5 first because follow-up questions go deep
- Technical Architects: Part 5 and Part 6 contain architecture decision content
Each part ends with a summary of tips, anti-patterns, and links to the next part.
Start with Part 1: Spring Boot Project Structure