Spring Data JPA
Let’s go over the Spring Data JPA, as well as its setup and integration with the H2 database.
We'll cover the following
What is Spring Data JPA?
The Spring Data JPA provides JPA-based data access layers via generic repositories. By implementing a Repository
interface in a Data Access Object (DAO), the DAO can have CRUD features with
Therefore, using the Spring Data JPA reduces a lot of boilerplate code, and less configuration is required to create the basic data access layer.
Setup
Let’s quickly refresh our understanding of the dependencies required for the database integration.
To use the Spring Data JPA in our Spring Boot application, todo
, we first need to add the spring-boot-starter-data-jpa
dependency in our build.gradle
. Also, for the database engine, we’ll need to add the H2
dependency.
plugins {id 'org.springframework.boot' version '2.6.2'id 'io.spring.dependency-management' version '1.0.11.RELEASE'id 'java'}group = 'io.educative'version = '0.0.1-SNAPSHOT'sourceCompatibility = '1.8'repositories {mavenCentral()}dependencies {implementation 'org.springframework.boot:spring-boot-starter-data-jpa'implementation 'org.springframework.boot:spring-boot-starter-validation'implementation 'org.springframework.boot:spring-boot-starter-web'compileOnly 'org.projectlombok:lombok'runtimeOnly 'com.h2database:h2'annotationProcessor 'org.projectlombok:lombok'testImplementation 'org.springframework.boot:spring-boot-starter-test'}test {useJUnitPlatform()}
H2 database configuration
In this section, we’ll take a close look at the setup and configuration of the H2 database in our todo
application.
The H2 setup
Now that we’ve added the H2 database engine in the build.gradle
, let’s discuss a few spring.datasource
properties to configure the database URL and credentials.
Let’s look at the application.properties
:
spring.datasource.url=jdbc:h2:~/testdbspring.datasource.driverClassName=org.h2.Driverspring.datasource.username=saspring.datasource.password=passwordspring.jpa.database-platform=org.hibernate.dialect.H2Dialect
Alternatively, we can also use application.yml
for similar Spring configurations.
spring:datasource:url: jdbc:h2:~/testdbusername: sapassword: passworddriverClassName: org.h2.Driverjpa:database-platform: org.hibernate.dialect.H2Dialect
To keep this course simple, we’ve integrated the H2 database engine to provide the in-memory data source to our todo
application. Of course, we can integrate other databases like MySQL and PostgreSQL by using the relevant database connector dependencies.
The H2 console
Once the configuration for the H2 database is added, we can restart the application and connect it to the database. As an extra feature of our application that proves quite handy in its development, the H2 embedded database also provides an built-in, UI-based console to access the database using the browser.
We can enable the H2 console by using the spring.h2.console.enabled
property in the application.properties
/application.yml
.
spring.datasource.url=jdbc:h2:~/testdbspring.datasource.driverClassName=org.h2.Driverspring.datasource.username=saspring.datasource.password=passwordspring.jpa.database-platform=org.hibernate.dialect.H2Dialectspring.h2.console.enabled=true
Hibernate setup
Further, let’s discuss a few useful properties to configure extra features of Hibernate in our todo
application.
spring.datasource.url=jdbc:h2:~/testdbspring.datasource.driverClassName=org.h2.Driverspring.datasource.username=saspring.datasource.password=passwordspring.jpa.database-platform=org.hibernate.dialect.H2Dialectspring.h2.console.enabled=truespring.jpa.show-sql=truespring.jpa.properties.hibernate.format_sql=truespring.jpa.hibernate.ddl-auto=create
For instance, by using the spring.jpa.show-sql
property we can enable Hibernate to show the underlying SQL. Similarly, SQL formatting is configurable in Hibernate with the spring.jpa.properties.hibernate.format_sql
property.
Last, we can enable Hibernate to manage the database schema by using the spring.jpa.hibernate.ddl-auto
property. It allows values like create
, update
, create-drop
, and validate
to create, update, drop, and validate the database schema automatically (based on the entities available).