Spring Boot JPA + H2 example: Build a CRUD Rest APIs
Tags: springboot, spring, jpa, java
Published: 2020-12-22 03:03:49

In this tutorial, we’re gonna build a Spring Boot Rest CRUD API example with Maven that use Spring Data JPA to interact with H2 database. You’ll know:

* How to configure Spring Data, JPA, Hibernate to work with Database
* How to define Data Models and Repository interfaces
* Way to create Spring Rest Controller to process HTTP requests
* Way to use Spring Data JPA to interact with H2 Database

For more details, please visit:
[https://bezkoder.com/spring-boot-jpa-h2-example/](https://bezkoder.com/spring-boot-jpa-h2-example/)

Using JdbcTemplate instead:
<a href="https://www.bezkoder.com/spring-boot-jdbctemplate-crud-example/">Spring Boot JdbcTemplate example: CRUD Rest API</a>

## Overview of Spring Boot JPA + H2 example
We will build a Spring Boot Rest Apis using Spring Data JPA with H2 Database for a Tutorial application in that:

* Each Tutotial has id, title, description, published status.
* Apis help to create, retrieve, update, delete Tutorials.
* Apis also support custom finder methods such as find by published status or by title.

These are APIs that we need to provide:

- POST `/api/tutorials`: create new Tutorial
- GET `/api/tutorials`: retrieve all Tutorials
- GET `/api/tutorials/[id]`: retrieve a Tutorial by :id
- PUT `/api/tutorials/[id]`: update a Tutorial by :id
- DELETE `/api/tutorials/[id]`: delete a Tutorial by :id
- DELETE `/api/tutorials`: delete all Tutorials
- GET `/api/tutorials/published`: find all published Tutorials
- GET `/api/tutorials?title=[keyword]`: find all Tutorials which title contains keyword

– We make CRUD operations & finder methods with Spring Data JPA’s JpaRepository.
– The database will be H2 Database (in memory or on disk) by configuring project dependency & datasource.

## Run the Spring Boot JPA + H2 example 
Run Spring Boot application with command: `mvn spring-boot:run`.
**_tutorials_** table will be automatically generated in Database.

Let's open H2 console with url: `http://localhost:8080/h2-ui`:
- For In-memory database:

![spring-boot-jpa-h2-example-crud-rest-api-login-h2-console-in-memory](https://dev-to-uploads.s3.amazonaws.com/i/og8t0ahh64e8ut35cunn.png)

- For on Disk database:

![spring-boot-jpa-h2-example-crud-rest-api-login-h2-console-on-disk](https://dev-to-uploads.s3.amazonaws.com/i/zagsdc5t4nvyoymmqyel.png)

Click on **Connect** button, then check H2 database, you can see things like this:

![spring-boot-jpa-h2-example-crud-rest-api-database-table](https://dev-to-uploads.s3.amazonaws.com/i/ho935wv6j3hd9wuexpmc.png)

Create some Tutorials:

![spring-boot-jpa-h2-database-example-crud-create-tutorial](https://dev-to-uploads.s3.amazonaws.com/i/ne2p1oqi5k7ap44md5h9.png)

H2 database `tutorials` table after that:

![spring-boot-jpa-h2-database-example-crud-create-tutorial-database-table](https://dev-to-uploads.s3.amazonaws.com/i/tgx9ae8gru1urr098caa.png)

Update some Tutorials:

![spring-boot-jpa-h2-database-example-crud-update-tutorial](https://dev-to-uploads.s3.amazonaws.com/i/3d414m12hk3hdeou29wu.png)

The table data is changed:

![spring-boot-jpa-h2-database-example-crud-retrieve-tutorial-database](https://dev-to-uploads.s3.amazonaws.com/i/4uhp6vjme0qw12u2tr61.png)

Retrieve all Tutorials:

![spring-boot-jpa-h2-database-example-crud-retrieve-all-tutorial](https://dev-to-uploads.s3.amazonaws.com/i/r8qpi93yh0ij94pddu4k.png)

Retrieve a Tutorial by Id:

![spring-boot-jpa-h2-database-example-crud-retrieve-one-tutorial](https://dev-to-uploads.s3.amazonaws.com/i/nskt265i7s9uvhq31nok.png)

Find all _**published**_ Tutorials:

![spring-boot-jpa-h2-database-example-crud-search-tutorial-by-status](https://dev-to-uploads.s3.amazonaws.com/i/a8bp346oybg70qu6goxg.png)

Find all Tutorials which title contains string 'ot':

![spring-boot-jpa-h2-database-example-crud-search-tutorial](https://dev-to-uploads.s3.amazonaws.com/i/62rqeq0oocliygzi2mnv.png)

Delete a Tutorial:

![spring-boot-jpa-h2-database-example-crud-delete-one-tutorial](https://dev-to-uploads.s3.amazonaws.com/i/apx0blf3s9vsosuapje2.png)

![spring-boot-jpa-h2-database-example-crud-delete-one-tutorial-database](https://dev-to-uploads.s3.amazonaws.com/i/ktw8363q3kknxjxjyhwz.png)

Delete all Tutorials:

![spring-boot-jpa-h2-database-example-crud-delete-all-tutorial](https://dev-to-uploads.s3.amazonaws.com/i/gr5g6b200cauymqqmloa.png)

H2 database table is clean now:

![spring-boot-jpa-h2-database-example-crud-delete-all-tutorial-database](https://dev-to-uploads.s3.amazonaws.com/i/d244hqxx1u79ni7qxkjg.png)

## Technology
* Java 8
* Spring Boot 2.4 (with Spring Web MVC, Spring Data JPA)
* H2 Database
* Maven 3.6.1

## Project Structure

![spring-boot-jpa-h2-database-example-crud-project-structure](https://dev-to-uploads.s3.amazonaws.com/i/tibyz03vwo5ihiaulz52.png)

Let me explain it briefly.

– `Tutorial` data model class corresponds to entity and table tutorials.
– `TutorialRepository` is an interface that extends `JpaRepository` for CRUD methods and custom finder methods. It will be autowired in `TutorialController`.
– `TutorialController` is a RestController which has request mapping methods for RESTful requests such as: *getAllTutorials*, *createTutorial*, *updateTutorial*, *deleteTutorial*, *findByPublished*…
– Configuration for Spring Datasource, JPA & Hibernate in **application.properties**.
– **pom.xml** contains dependencies for Spring Boot and H2 Database.

## Source Code
Tutorial link with details, implementation (step by step) and Github:
[https://bezkoder.com/spring-boot-jpa-h2-example/](https://bezkoder.com/spring-boot-jpa-h2-example/)

JdbcTemplate instead:
<a href="https://www.bezkoder.com/spring-boot-jdbctemplate-crud-example/">Spring Boot JdbcTemplate example: CRUD Rest API</a>

# Further Reading
Using other databases:
– [Spring JPA + PostgreSQL](https://bezkoder.com/spring-boot-postgresql-example/)
– [Spring JPA + MySQL](https://bezkoder.com/spring-boot-jpa-crud-rest-api/)
– [Spring Data + MongoDB](https://bezkoder.com/spring-boot-mongodb-crud/)

If you want to add Pagination to this Spring project, you can find the instruction at:
[Spring Boot Pagination & Filter example | Spring JPA, Pageable](https://bezkoder.com/spring-boot-pagination-filter-jpa-pageable/)

To sort/order by multiple fields:
[Spring Data JPA Sort/Order by multiple Columns | Spring Boot](https://bezkoder.com/spring-data-sort-multiple-columns/)

Handle Exception for this Rest APIs is necessary:
– [Spring Boot @ControllerAdvice & @ExceptionHandler example](https://bezkoder.com/spring-boot-controlleradvice-exceptionhandler/)
– [@RestControllerAdvice example in Spring Boot](https://bezkoder.com/spring-boot-restcontrolleradvice/)

Or way to write Unit Test for the JPA Repository:
[Spring Boot Unit Test for JPA Repositiory with @DataJpaTest](https://bezkoder.com/spring-boot-unit-test-jpa-repo-datajpatest/)