...

/

Database Integration: Spring Data

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 ...