Database Integration: Spring Data
In this lesson, you will learn about integrating relational databases to our Spring Boot application.
What is Java ORM?
Java ORM (Object-Relation Mapping) is a programming technique that allows us to store, query, and manipulate Java objects stored in relational databases. This is done by mapping the Java object fields with the column of database tables.
The most common and widely used implementation of Java ORM is Hibernate.
What is JPA?
Java Persistence API (or JPA) is a specification that provides us with a collection of classes and methods to store, query, and manipulate data stored in databases. JPA facilitates the object-relational mapping between Java objects and relational databases and vice-versa.
What is Spring Data
Spring Data is a collection of libraries that make it easy for us to access, query and manipulate relational and non-relational databases. Spring Data provides easy integration with Spring MVC, as well as easy configuration to create data sources or connections of different databases. Spring Data uses Hibernate for managing ORM between Java objects and relational databases. Spring REST, which is one of the Spring Data libraries, exposes the repositories as hypermedia-driven REST services.
What is the H2 database?
H2 is an open-source, lightweight database written in Java. It can be configured as in-memory, which does not persist the data onto the disk.
Integrating the H2 database
Dependencies to be added
The following dependencies are needed for accessing relational databases. For accessing H2, we need com.h2database:h2
, which can be replaced with a database-specific driver library for using a different database. For other databases, we use mysql:mysql-connector-java:8.0.20
for MySQL or org.postgresql:postgresql:42.2.14
for PostgreSQL.
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa' // mandatory
runtimeOnly 'com.h2database:h2' // database-specific
}
Creating the database tables
Spring Data uses file data.sql
to initialize the relational database on the application boot up. If we want to create tables and relationships between them, we can put the SQL scripts in data.sql
and place it under src/main/resources
, which will go into the classpath of the application. into the classpath of the application. This step is completely optional if the schemas and tables are already created. As we are using an in-memory database that gets reset every time the application is booted up, we are using it here.
Playlist – has fields id
, name
, and created_on
Song – has fields id
, playlist_id
, name
, cover_url
, and created_on
A playlist can contain multiple songs, and a song belongs to only one playlist.
CREATE TABLE IF NOT EXISTS playlist
(
id BIGINT auto_increment PRIMARY KEY,
name VARCHAR(250) NOT NULL,
created_on DATE DEFAULT sysdate
);
CREATE TABLE IF NOT EXISTS song
(
id BIGINT auto_increment PRIMARY KEY,
playlist_id BIGINT NOT NULL,
name VARCHAR(250) NOT NULL,
cover_url VARCHAR(250) NOT NULL,
created_on DATE DEFAULT sysdate,
FOREIGN KEY(playlist_id) REFERENCES playlist(id) ON UPDATE CASCADE
);
Creating a database entity object
With respect to the database tables, we create the following Java classes.
Get hands-on with 1400+ tech skills courses.