Foundation · Build
The JPA Ecosystem
JDBC speaks SQL, JPA defines the object-relational contract, Hibernate implements it, and Spring Data repositories sit on top—each layer has its own documentation and its own levers.
Primary source: Spring Data JPA: Core Concepts
Lesson 1 of 17
1d streakThe Architectural Question
Tuning searches land in the wrong manual: flush modes are Hibernate, derived queries are Spring Data, isolation is JDBC—and each layer owns different fixes.
What this guide proves
- JDBC
- Hibernate
- Spring Data JPA
Legacy persistence.xml vs Modern Auto-configuration
Before Spring Boot, JPA required a META-INF/persistence.xml file to define the persistence unit, specify database dialects, and list every single @Entity class manually.
<!-- Legacy JPA XML Configuration -->
<persistence xmlns="https://jakarta.ee/xml/ns/persistence" version="3.0">
<persistence-unit name="my-pu" transaction-type="RESOURCE_LOCAL">
<!-- Must manually list every entity! -->
<class>com.example.domain.User</class>
<class>com.example.domain.Order</class>
<properties>
<property name="jakarta.persistence.jdbc.driver" value="org.postgresql.Driver"/>
<property name="jakarta.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/db"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
</properties>
</persistence-unit>
</persistence>Architectural Boundary · Do Not Confuse
Manual persistence.xml
Spring Boot Auto-configuration
Today, Spring Boot's HibernateJpaAutoConfiguration automatically scans your packages for @Entity classes (via @EntityScan) and configures the EntityManagerFactory using properties from application.yml, completely eliminating persistence.xml.
JDBC vs JPA vs Hibernate vs Spring Data JPA: The 4-layer stack
A frequent point of confusion in backend Java engineering is distinguishing who owns what in the persistence stack. Spring Boot does not replace SQL or Hibernate; it coordinates a 4-layer abstraction pyramid:
| Layer | Role | Primary Artifacts | What it Owns |
|---|---|---|---|
| Layer 1: JDBC | Database Driver & Transport | Connection, PreparedStatement, ResultSet | Network I/O, raw SQL execution, database socket pools. |
| Layer 2: JPA (Jakarta Persistence) | Standard Specification (Interfaces) | EntityManager, EntityTransaction, @Entity | Specification only! Contains no implementation code. |
| Layer 3: Hibernate ORM | JPA Implementation (Engine) | Session, SessionFactory, ActionQueue, First-Level Cache | Object-Relational Mapping, SQL generation, dirty checking, caching. |
| Layer 4: Spring Data JPA | Repository Automation Abstraction | JpaRepository, SimpleJpaRepository, Querydsl | Generates runtime repository proxies, derived queries, and pagination. |
HikariCP connection pooling and database dialects
Spring Boot uses HikariCP as its default connection pool. HikariCP maintains a pool of open TCP socket connections, lending them per transaction and reclaiming them upon commit or rollback.
# HikariCP connection pool sizing
spring.datasource.hikari.maximum-pool-size=20
spring.datasource.hikari.minimum-idle=5
spring.datasource.hikari.idle-timeout=300000
spring.datasource.hikari.connection-timeout=20000
# Hibernate SQL dialect & formatting
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.show-sql=false
spring.jpa.properties.hibernate.format_sql=trueBreak it deliberately: Missing JDBC driver at runtime
If a database driver (such as postgresql or mysql-connector-j) is missing from the classpath, Spring Boot halts startup during DataSourceAutoConfiguration.
APPLICATION FAILED TO START *************************** Description: Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured. Reason: Failed to determine a suitable driver class: ClassNotFoundException: org.postgresql.Driver Action: Consider the following: If you want an embedded database (H2, HSQL or Derby), please put it on the classpath. To bind to PostgreSQL, add org.postgresql:postgresql to your pom.xml dependencies.
Dependency Scope Rule
Check your understanding
Active Recall Checkpoint
Does Jakarta Persistence (JPA) contain any implementation code that executes SQL?
Hint: Is it a specification or an implementation?
Show answer & explanation
No. JPA is purely an API specification (interfaces and annotations). Hibernate is the actual ORM engine that implements JPA and generates SQL.
Active Recall Checkpoint
What happens if the JDBC driver dependency is missing from a Spring Boot application connecting to PostgreSQL?
Hint: Can HikariCP initialize?
Show answer & explanation
Spring Boot fails startup immediately with a ClassNotFoundException on the database Driver class and aborts DataSource auto-configuration.
Runtime Architecture & Flow
Decision canvas
Evaluate the choice across three constraints
These are comparison criteria, not sequential steps. A strong answer states which constraint dominates in the current system.
JDBC
Ask how each option changes this guarantee, its operational cost, and its failure mode.
Hibernate
Ask how each option changes this guarantee, its operational cost, and its failure mode.
Spring Data JPA
Ask how each option changes this guarantee, its operational cost, and its failure mode.
Decision prompt: Remove Spring Data JPA from the POM and write a raw JDBC PreparedStatement to appreciate the boilerplate it saves.
Synthesis & Production Verification
Production Acceptance Criteria
- Place JDBC, JPA, Hibernate, and Spring Data on one diagram with their responsibilities
- Rewrite one repository call using only EntityManager methods
- Attribute a named feature (flushing, caching, derived queries) to its owning layer
Key Takeaway
JPA defines the standard ORM specification; Hibernate provides the runtime implementation, and Spring Data JPA synthesizes repository proxies on top of HikariCP.
Architectural & Technical Review
Core Interview Questions
Q01What is the relationship between JPA and Hibernate?Show answer
JPA is the Jakarta specification interface; Hibernate is the runtime ORM provider implementing that specification.
Q02How does HikariCP manage database connections in Spring Boot?Show answer
It maintains a fixed pool of persistent TCP sockets, lending connections per transaction and releasing them upon commit or rollback.
Q03What happens if the JDBC driver is missing from runtime dependencies?Show answer
Spring Boot cannot instantiate the HikariDataSource bean at startup and fails with a Cannot load driver class exception.
Production Pitfalls
Critical Anti-Patterns
- Pitfall: Confusing JPA specification with Hibernate ORM — Importing Hibernate proprietary classes instead of jakarta.persistence.* couples domain models to a specific vendor.
- Pitfall: Ignoring connection pool sizing — Default pool sizes may cause bottleneck under high traffic or exhaust database server maximum connection limits.
Primary Documentation & Specifications
Lesson Primary References
Supplementary Module Resources