Skillversitas

Spring Data JPA

Introduction

Spring Data JPA simplifies database access by providing an abstraction on top of JPA (Java Persistence API). Spring Boot integrates JPA automatically, so the configuration is minimal.

What Is JPA?

JPA (Java Persistence API) is a Java specification for mapping Java objects to a relational database. JPA is just a specification — implementations are provided by providers such as Hibernate, EclipseLink, or OpenJPA.

Hibernate as a JPA Provider

Spring Boot uses Hibernate as the default JPA provider. Hibernate handles:

  • Object-to-database-table mapping (ORM)
  • Automatic SQL generation
  • Session management
  • Caching

Database Configuration

application.properties:

# H2 Database (in-memory)
spring.datasource.url=jdbc:h2:mem:app
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=

# Hibernate
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true

# H2 Console
spring.h2.console.enabled=true

application.properties for MySQL:

spring.datasource.url=jdbc:mysql://localhost:3306/app
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=secret

spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
spring.jpa.hibernate.ddl-auto=validate
spring.jpa.show-sql=true

ddl-auto Options

Value Purpose
create Create new tables every time the application starts
create-drop Create tables, drop them when the application stops
update Update the schema if there are changes
validate Validate the schema, don't modify the database
none Do nothing

Why Spring Data JPA?

Without Spring Data JPA:

public interface UserRepository {
    User findById(Long id);
    List<User> findAll();
    User save(User user);
    void deleteById(Long id);
}

You'd need a long manual implementation using Hibernate Session/EntityManager.

With Spring Data JPA:

public interface UserRepository extends JpaRepository<User, Long> {
}

You immediately get findById(), findAll(), save(), and deleteById() without any implementation.

Spring Boot Dependency

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>runtime</scope>
</dependency>

Spring Data JPA Architecture

Controller → Service → Repository → Database
                ↓
           Entity (Model)
  • Entity: Representation of a database table
  • Repository: Interface for data access
  • Service: Business logic
  • Controller: Handles HTTP requests

Summary

  • Spring Data JPA minimizes data-access boilerplate code.
  • Hibernate is the default JPA provider for Spring Boot.
  • JpaRepository provides CRUD automatically.

Next chapter: We'll cover JPA Entity & Repository — how to define entities and create repositories.