Spring Boot DATA JDBC CRUD Web App

DATA JPA vs DATA JDBC

In the previous post Spring Boot JPA Crud Web App, we have implemented CRUD functionality using spring boot’s data-jpa module.

Spring Data JPA is very rich in features but unfortunately it is not widely preferred due to its internal complexities and the latency costs involved in getting the work done.
If you are looking for an alternative, then you can consider Spring Data JDBC.

Here are some of the cool things about this module:

  • Spring Data JDBC is much simpler, cleaner and a straight-forward model.
  • The SQL commands are executed only when invoked by a repository method.
  • You get a fully loaded object / entity instance as a result for your query – forget about session, proxies, lazy-loading or caching.
  • In order to be simple, it does NOT offer caching, lazy loading, write behind and many other features of JPA
  • All these things make is a simple, limited and better choice compared to JPA for certain use-cases.

01. Add spring-data-jdbc dependency

Remove spring-boot-starter-data-jpa dependency and add the below dependency.

1
2
3
4
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jdbc</artifactId>
</dependency>

02. Update Entity Definition

Remove javax.persistence annotations and update Product.java as show below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package snmaddula.product.crud.entity;

import org.springframework.data.annotation.Id;

import lombok.Data;

@Data
public class Product {

@Id
private Long id;
private String title;
private String description;
private Double price;

}

In case, if your lombok configuration is not working, you can define setters & getters in the above class.

03. Repository Definition

Instead of JpaRepository, our ProductRepository would now extend CrudRepository as below.

1
2
3
4
5
6
7
8
9
10
package snmaddula.product.crud.repo;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import snmaddula.product.crud.entity.Product;

@Repository
public interface ProductRepo extends CrudRepository<Product, Long> {
}

04. Service Implementation

Refer to ProductService.java from previous post and update the below methods accordingly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package snmaddula.product.crud.service;

import java.util.List;
import static java.util.stream.Collectors.toList;
import static java.util.stream.StreamSupport.stream;

import org.springframework.stereotype.Service;

import lombok.RequiredArgsConstructor;
import snmaddula.product.crud.entity.Product;
import snmaddula.product.crud.repo.ProductRepo;

@Service
@RequiredArgsConstructor
public class ProductService {

private final ProductRepo productRepo;

public List<Product> fetchAll() {
return stream(productRepo.findAll().spliterator(), false).collect(toList());
}

public void removeAll() {
productRepo.deleteAll();
}

}

05. CREATE schema.sql

As JDBC does not auto create tables, we need to create table on our own.
Add the below CREATE query in your schema.sql file and place this file on the classpath.

1
2
3
4
5
6
CREATE TABLE PRODUCT (
ID INT PRIMARY KEY AUTO_INCREMENT,
TITLE TEXT,
DESCRIPTION TEXT,
PRICE DECIMAL
);