Spring Data JPA
Database Configuration, Datasource, and Persistence
Spring Data JPA is a module within the Spring Data ecosystem providing a high-level abstraction for managing relational data persistence, drastically reducing data access boilerplate code.
1. What is Spring Data and how to install it?
Spring Data standardizes access to relational and NoSQL databases. Spring Data JPA extends the JPA specification to automate repository creation via interface contracts.
1.1. Maven and Gradle Dependencies
In Maven (pom.xml):
<dependencies>
<!-- Spring Data JPA Starter (includes Hibernate, Spring Data, and JPA) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- H2 Database for development/testing -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<!-- PostgreSQL Driver for production/relational development -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
In Gradle (build.gradle):
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
runtimeOnly 'com.h2database:h2'
runtimeOnly 'org.postgresql:postgresql'
}
After adding Spring Data JPA starters and JDBC drivers (h2 or postgresql), verify that persistence artifacts download cleanly to your local repository by running:
- Maven:
./mvnw clean install(ormvn clean install) - Gradle:
./gradlew build(or./gradlew --refresh-dependencies)
2. Detailed Breakdown of application.properties
# ==========================================
# 1. DATASOURCE CONFIGURATION (PostgreSQL)
# ==========================================
# JDBC connection URL to PostgreSQL
spring.datasource.url=jdbc:postgresql://localhost:5432/boardgame
# PostgreSQL username
spring.datasource.username=postgres
# Database password
spring.datasource.password=postgres
# Official PostgreSQL JDBC Driver
spring.datasource.driver-class-name=org.postgresql.Driver
# Hibernate Dialect for optimizing SQL generation toward PostgreSQL
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
# ==========================================
# 2. ALTERNATIVE: H2 DATABASE (In-Memory or File)
# ==========================================
# spring.datasource.driver-class-name=org.h2.Driver
# Option A: H2 In-Memory RAM
# spring.datasource.url=jdbc:h2:mem:db-kevin;DB_CLOSE_DELAY=-1
# Option B: H2 File-Persisted (creates ./db-kevin.mv.db)
# spring.datasource.url=jdbc:h2:./db-kevin;DB_CLOSE_DELAY=-1
# Default H2 credentials
# spring.datasource.username=postgres
# spring.datasource.password=postgres
# ==========================================
# 3. SCHEMA MANAGEMENT WITH HIBERNATE (ddl-auto)
# ==========================================
# Strategy for automatic DDL schema generation:
# - none: No modifications.
# - validate: Validates tables against JPA entities.
# - update: Updates schema adding missing columns/tables.
# - create: Creates schema on startup, dropping existing tables.
# - create-drop: Creates schema on startup and drops it when JVM shuts down.
spring.jpa.hibernate.ddl-auto=create-drop
# ==========================================
# 4. DATA INITIALIZATION WITH SQL SCRIPTS
# ==========================================
# Controls execution mode for schema.sql and data.sql:
# - always: Always run SQL scripts on startup.
# - never: Never run automatic SQL scripts.
spring.sql.init.mode=always
# Show or hide SQL queries in console logs
spring.jpa.show-sql=false
# Deferred DataSource initialization:
# When set to true, forces Spring Boot to wait for Hibernate DDL table creation
# BEFORE attempting to execute data.sql scripts.
spring.jpa.defer-datasource-initialization=true
# ==========================================
# 5. H2 WEB CONSOLE
# ==========================================
# Enables the interactive browser console for inspecting H2 databases
spring.h2.console.enabled=true
# URI path for H2 console (e.g., http://localhost:8080/h2-console)
spring.h2.console.path=/h2-console
DB_CLOSE_DELAY=-1 Essential in H2?By default, in-memory H2 databases drop all tables and destroy content as soon as the last active JDBC connection closes. Setting ;DB_CLOSE_DELAY=-1 in the JDBC connection string keeps the in-memory database alive throughout the Spring Boot JVM process lifecycle.
3. Initial Data Seeding with data.sql
When spring.jpa.defer-datasource-initialization=true and spring.sql.init.mode=always are set, Spring Boot automatically picks up data.sql from src/main/resources/.
-- Initial data seed script executed automatically at application startup
INSERT INTO usuarios (nombre, email) VALUES ('Kevin', 'kevin@icesi.edu.co');
INSERT INTO usuarios (nombre, email) VALUES ('Ana', 'ana@icesi.edu.co');
INSERT INTO usuarios (nombre, email) VALUES ('Carlos', 'carlos@icesi.edu.co');
4. Step-by-Step Practical Guide
Include starters in build file
Verify that spring-boot-starter-data-jpa and h2 or postgresql drivers are declared in pom.xml or build.gradle.
Configure H2 in-memory in application.properties
Set spring.datasource.url=jdbc:h2:mem:db-kevin;DB_CLOSE_DELAY=-1 and enable spring.h2.console.enabled=true.
Set DDL strategy and deferred initialization
Set spring.jpa.hibernate.ddl-auto=create-drop and spring.jpa.defer-datasource-initialization=true.
Create data.sql file
Place data.sql inside src/main/resources/ with INSERT statements to populate initial rows.
Verify records in H2 Console
Start the app, navigate to http://localhost:8080/h2-console, enter JDBC URL jdbc:h2:mem:db-kevin, and query tables.