Spring Boot

Article 7: Spring Boot + JPA + Hibernate (Database Basics)

By Utility Zone · 2026-01-27T18:29:49.641672

1. Introduction

So far, our application has been stateless. Real applications need to store and retrieve data.

Spring Boot + JPA + Hibernate allow you to:

  • Map Java objects to database tables
  • Avoid writing boilerplate SQL
  • Work at a higher level of abstraction

This article introduces database integration step by step.


2. What Is JPA and Hibernate?

2.1 JPA (Java Persistence API)

  • A specification (not an implementation)
  • Defines how Java objects map to relational databases

2.2 Hibernate

  • A JPA implementation
  • Does the actual ORM work

Spring Boot uses Hibernate by default when JPA is added.


3. Choosing a Database for Learning

For learning:

  • ✅ H2 (In‑Memory Database)

For real projects:

  • MySQL
  • PostgreSQL

We’ll start with H2 to keep things simple.


4. Adding JPA Dependencies

Add these to pom.xml:

<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 Boot will auto-configure:

  • DataSource
  • EntityManager
  • Transaction manager

5. Database Configuration

Add to application.properties:

spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=

spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=update

spring.h2.console.enabled=true
spring.h2.console.path=/h2-console

6. Creating Your First Entity

6.1 What Is an Entity?

An entity is a Java class mapped to a database table.


6.2 Create User Entity

package com.example.demo.model;

import jakarta.persistence.*;

@Entity
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;
    private String email;

    public User() {}

    public User(String name, String email) {
        this.name = name;
        this.email = email;
    }

    // getters and setters
}

Hibernate will automatically create a table.


7. Understanding Key Annotations

AnnotationPurpose
@EntityMarks class as DB table
@IdPrimary key
@GeneratedValueAuto-generated ID
@ColumnColumn mapping (optional)

8. Verifying Database Setup

Start the app and open:

http://localhost:8080/h2-console

JDBC URL:

jdbc:h2:mem:testdb

Click Connect.

You should see the USER table.


9. ddl-auto Explained (Important)

spring.jpa.hibernate.ddl-auto=update

Options:

  • create → recreates tables every start
  • update → safe for development
  • validate → checks schema
  • none → no schema changes

Use:

  • update → dev
  • migrations → prod

10. Common Beginner Mistakes

❌ Forgetting no-arg constructor
❌ Missing @Entity
❌ Wrong package scanning
❌ Using create in production


11. What You Should Understand Before Moving On

You should now know:

  • What JPA and Hibernate are
  • How entities map to tables
  • How Spring Boot auto-configures databases
  • How to access H2 console

12. What’s Next?

➡ Article 8: Spring Data JPA Repositories

  • JpaRepository
  • CRUD operations
  • Query methods

Type Next when you’re ready.