Spring Boot Data JPA CRUD Example

Spring Data JPA CRUD

You will see here Spring Boot Data JPA CRUD example. CRUD is an acronym that means Create Read Update and Delete operations. I will use an in-memory h2 database in order to perform CRUD operations in database. I have other example on Spring Data JPA CRUD example but the application does not use Spring Boot. In this example, I will use Spring Boot framework in this example. I will use Spring REST controller to expose the CRUD operations into REST API.


Java 1.8/19, Gradle 5.4.1, Maven 3.8.5, Spring Boot 3.2.4

Please go through the following sections in order to implement the Spring Boot Data JPA CRUD example.

Project Setup

I will create here maven based project and the following pom.xml file can be used for your project.

In the below build file I have included spring boot web, spring boot data jpa and h2 database. The in-memory h2 database is very useful when you need to do a quick PoC because it saves you a lots of time and effort required to setup a database.

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">









Entity Class

I will work with in-memory h2 database, where I will perform CRUD operations. Therefore I need to create entity class to map the database column with Java class attributes.

I want to store website details, such as, website id, name and URL of the website.

Another beauty of Spring Boot Data JPA starter is the below class will create the required table in the database. Even I didn’t mention any column name, so it will create same column name in the table as the Java attributes.

So the corresponding website entity class can be defined as:

package com.roytuts.spring.boot.data.jpa.crud.entity;

public class Website {
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Integer id;
	private String name;
	private String url;
	public Website() {
	public Website(Integer id, String name, String url) {
		this.id = id;
		this.name = name;
		this.url = url;
	//getters and setters

VO Class

It is always good idea to create a VO or DTO class to return as a response object from REST controller instead of returning the entity class as a response object.

The VO class is also similar to the above entity class.

package com.roytuts.spring.boot.data.jpa.crud.vo;

public class WebsiteVo {
	private Integer id;
	private String name;
	private String url;
	public WebsiteVo() {
	public WebsiteVo(Integer id, String name, String url) {
		this.id = id;
		this.name = name;
		this.url = url;
        //getters and setters

Repository Interface

Spring Data JPA provides built-in methods for performing basic CRUD operations and you don’t need to write any query method.

Another beauty of Spring Data JPA is that you write Java method to query your database and Spring generates the corresponding query from your method for performing database operations.

I have just extended the JpaRepository interface but I did not write any query method inside this repository because we will use Spring’s built-in methods for performing CRUD operations.

package com.roytuts.spring.boot.data.jpa.crud.repository;

public interface WebsiteRepository extends JpaRepository<Website, Integer> {

Service Class

The service class is used generally if you want to perform some business logic or conversion to different data.

Here in the below service method I will convert entity class to VO class using Java 8’s stream API.

I have used Java 8’s map() method to map entity to VO object.

package com.roytuts.spring.boot.data.jpa.crud.service;

public class WebsiteService {
	private WebsiteRepository repository;
	public List<WebsiteVo> getWebsiteList() {
		return repository.findAll().stream().map(w -> {
			WebsiteVo vo = new WebsiteVo(w.getId(), w.getName(), w.getUrl());
			return vo;
	public WebsiteVo getWebsiteById(Integer id) {
		return repository.findById(id).map(w -> {
			WebsiteVo vo = new WebsiteVo(w.getId(), w.getName(), w.getUrl());
			return vo;
	public void saveWebsite(WebsiteVo vo) {
		Website website = new Website();
	public void updateWebsite(WebsiteVo vo) {
		Website website = new Website(vo.getId(), vo.getName(), vo.getUrl());
	public void deleteWebsite(WebsiteVo vo) {
		Website website = new Website(vo.getId(), vo.getName(), vo.getUrl());

REST Controller

Now I will expose our CRUD operations onto REST API so that any client can call our REST service and perform CRUD operations.

Exposing the operations on REST makes loosely coupled to integrate with any client technologies.

Notice how I have performed CRUD operations using the same endpoint with different HTTP methods.

package com.roytuts.spring.boot.data.jpa.crud.rest.controller;

public class WebsiteRestController {
	private WebsiteService service;
	public ResponseEntity<WebsiteVo> getWebsite(@PathVariable Integer id) {
		return new ResponseEntity<WebsiteVo>(service.getWebsiteById(id), HttpStatus.OK);
	public ResponseEntity<List<WebsiteVo>> getWebsiteList() {
		return new ResponseEntity<List<WebsiteVo>>(service.getWebsiteList(), HttpStatus.OK);
	public ResponseEntity<String> saveWebsite(@RequestBody WebsiteVo websiteVo) {
		return new ResponseEntity<String>("New website successfully saved", HttpStatus.OK);
	public ResponseEntity<String> updateWebsite(@RequestBody WebsiteVo websiteVo) {
		return new ResponseEntity<String>("New website successfully updated", HttpStatus.OK);
	public ResponseEntity<String> deleteWebsite(@RequestBody WebsiteVo websiteVo) {
		return new ResponseEntity<String>("New website successfully deleted", HttpStatus.OK);

Application Properties File

I will create an application.yml file that will hold some configurations. You can also create application.properties file. This file should be put under src/main/resources folder.

The below YAML or properties file contains SQL format and h2 database console enablement to see the queries executed during performing of the CRUD operations.

      show-sql: true
         enabled: true

The following content is written into the application.properties file:


Main Class

Creating a main would be sufficient to deploy our application into the Tomcat server. This is a great advantage that you just need to let Spring know that it is your Spring Boot Application using @SpringBootApplication and main class.

I also need to tell Spring where the entity class and repository interface exist.

package com.roytuts.spring.boot.data.jpa.crud;

public class SpringBootDataJpaCrudApp {
	public static void main(String[] args) {
		SpringApplication.run(SpringBootDataJpaCrudApp.class, args);

Testing the CRUD Application

Run the main class to start the application.

Creating Website

Create websites using REST client as shown below:

spring boot data jpa crud example

In the console you will see below output:

Hibernate: insert into website (id, name, url) values (null, ?, ?)

Reading Websites

So when you make the below call to see list of websites then you will get below output:

spring boot data jpa crud example

You will get below output in the console:

Hibernate: select website0_.id as id1_0_, website0_.name as name2_0_, website0_.url as url3_0_ from website website0_

Updating Website

Now let’s say we want to update website with below information:

spring boot data jpa crud example

You will find console output:

Hibernate: update website set name=?, url=? where id=?

Deleting Website

Send below information to delete a website:

spring boot crud example

You will see below output in the console:

Hibernate: delete from website where id=?

Source Code


Old source code

2 thoughts on “Spring Boot Data JPA CRUD Example

Leave a Reply

Your email address will not be published. Required fields are marked *