🗄️ Spring Data JPA & Hibernate — Complete Interview Notes

Spring Data JPA simplifies database access in Spring Boot applications by providing an abstraction layer over the Hibernate ORM implementation of the Java Persistence API. It allows developers to interact with relational databases using Java objects instead of writing complex SQL queries.

spring_data

Spring Data Jpa

Prerequisites:

Before starting the implementation, ensure you have the following installed:

• Java 8 or higher • Mysql Database • Any IDE such as IntelliJ IDEA, Eclipse IDE



1. JPA vs Hibernate vs Spring Data JPA

Conceptual Hierarchy

Spring Data JPA   (abstraction layer — repositories, query derivation)
      ↓
   JPA API        (javax.persistence / jakarta.persistence — specification)
      ↓
  Hibernate       (JPA implementation — actual ORM engine)
      ↓
   JDBC           (raw DB connection)
Feature JPA Hibernate Spring Data JPA
Type Specification (interface) Implementation Abstraction
Package jakarta.persistence org.hibernate org.springframework.data
Provides Annotations, JPQL HQL, Session, Caching Repositories, Query derivation
Standalone ❌ (needs JPA impl)

application.properties — Baseline Config

# DataSource
spring.datasource.url=jdbc:postgresql://localhost:5432/mydb
spring.datasource.username=postgres
spring.datasource.password=secret
spring.datasource.driver-class-name=org.postgresql.Driver

# JPA / Hibernate
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=validate        # prod: validate | dev: update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.use_sql_comments=true

# HikariCP
spring.datasource.hikari.maximum-pool-size=10
spring.datasource.hikari.minimum-idle=2
spring.datasource.hikari.connection-timeout=30000

2. Entity Lifecycle & States