Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

Connecting a database

Posts kept in memory vanish on every restart. Time to store them in a database. In the tutorial you worked with a database through JDBC: wrote SQL, read a ResultSet, closed connections. In Spring Boot most of this is done for you by three technologies working together.

Who is who

NameWhat it is
JPA (Jakarta Persistence)A standard: annotations and rules for linking a Java class to a table
HibernateA library that implements JPA and writes SQL itself
Spring Data JPAA Spring layer: repositories without an implementation, queries from method names

You describe an entity class and a repository interface, and Hibernate turns method calls into SQL queries.

Dependencies

Add three blocks to pom.xml (on start.spring.io: Spring Data JPA and H2 Database):

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

H2 is a database written in Java. It needs no separate server: it keeps data in a file inside the project. That is convenient for development. The spring-boot-h2console module adds a web console for viewing tables.

Settings

# src/main/resources/application.yaml
spring:
  datasource:
    url: jdbc:h2:file:./data/blog
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true
    open-in-view: false
  h2:
    console:
      enabled: true
SettingWhat it does
datasource.urlThe database address: the data/blog.mv.db file in the project folder
ddl-auto: updateHibernate creates and extends tables from the entities itself
show-sql: truePrints every SQL query to the log
open-in-view: falseCloses database access once the service returns — so mistakes show up at once
h2.console.enabledSwitches on the H2 console

The ddl-auto: update mode is only for the early days — in the migrations lesson we replace it with Flyway. Add the data/ folder to .gitignore so the database file does not end up in Git.

Starting up

The log shows lines about the database (shortened):

INFO ... HHH000001: Hibernate ORM core version 7.4.5.Final
INFO ... HikariPool-1 - Added connection conn0: url=jdbc:h2:file:./data/blog user=
INFO ... H2 console available at '/h2-console'. Database available at 'jdbc:h2:file:./data/blog'

HikariCP is a connection pool: it keeps several database connections open and hands them out to requests. Open http://localhost:8080/h2-console, enter jdbc:h2:file:./data/blog in the JDBC URL field, leave User Name empty and click Connect — a console with the tables opens.

PostgreSQL instead of H2

Servers usually run PostgreSQL. It needs a driver:

<!-- pom.xml -->
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <scope>runtime</scope>
</dependency>

and different connection settings:

# src/main/resources/application-prod.yaml
spring:
  datasource:
    url: jdbc:postgresql://localhost:5432/blog
    username: blog
    password: changeme

The entity and repository code stays the same — Hibernate adapts the SQL to the database itself. The real password is not written into the file; the deployment chapter covers this.

Common mistake

A file-based H2 database can be opened by only one program. If the application is already running and you connect to the same file from another program or start a second copy, you get:

Database may be already in use: ".../data/blog.mv.db". Possible solutions: close all other connection(s); use the server mode [90020-240]

Stop the extra process. While the application runs, look at the database through its own /h2-console.

Thanks for reading!

Donate to the site — we work hard for you.