In tradition approach, implementing Data Access Layer makes lots of boilerplate code. Spring Data provides us Spring JPA which supports the ways to write interface for repositories and custom finder methods. The implementation will be done automatically by Spring Framework.
The tutorial shows you how to execute asynchronous query with Spring JPA and PostgreSQL using Spring Boot.
Related articles:
– Java 8 Streams
– How to use Spring JPA MySQL | Spring Boot
– How to use Spring JPA with PostgreSQL | Spring Boot
– How to get streaming results with Spring JPA, Java 8 Stream and PostgreSQL | Spring Boot
– @DataJPATest with Spring Boot
I. Technology
– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot: 1.5.1.RELEASE
II. Overview
1. Project Structure
– Class Customer corresponds to entity and table customer, it should be implemented Serializable.
– CustomerRepository is an interface extends CrudRepository, will be autowired in WebController for implementing repository methods and custom finder methods.
– WebController is a REST Controller which has request mapping methods for RESTful requests such as: save, findall, findonebyid, findbylastname.
– Configuration for Spring Datasource and Spring JPA properties in application.properties
– Dependencies for Spring Boot and PostgreSQL in pom.xml
2. Step to do
– Create Spring Boot project & add Dependencies
– Configure Spring JPA
– Create DataModel Class
– Create Spring JPA Repository Interface
– Create Web Controller
– Create PostGreSQL table
– Run Spring Boot Application & Enjoy Result
III. Practice
1. Create Spring Boot project & add Dependencies
Open Spring Tool Suite, on Menu, choose File -> New -> Spring Starter Project, then fill each fields.
Click Next, in SQL: choose JPA and PostgreSQL, in Web: choose Web.
Click Finish, then our project will be created successfully.
Open pom.xml and check Dependencies:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <scope>runtime</scope> </dependency>
These dependencies were auto-generated by the configuration we have done before.
2. Configure Spring JPA
Open application.properties
spring.datasource.url=jdbc:postgresql://localhost/testdb spring.datasource.username=postgres spring.datasource.password=123 spring.jpa.generate-ddl=true
3. Create DataModel Class
Under package model, create class Customer.
Content of Customer.java:
package com.javasampleapproach.jpaasync.model; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name = "customer") public class Customer implements Serializable { private static final long serialVersionUID = -3009157732242241606L; @Id @GeneratedValue(strategy = GenerationType.AUTO) private long id; @Column(name = "firstname") private String firstName; @Column(name = "lastname") private String lastName; protected Customer() { } public Customer(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } @Override public String toString() { return String.format("Customer[id=%d, firstName='%s', lastName='%s']", id, firstName, lastName); } }
Annotation @Entity indicates that Customer is an Entity and @Table specifies the primary table (name customer) for the annotated @Entity.
@ID specifies the primary key and @GeneratedValue indicates generation strategy for value of primary key.
@Column: mapped column (in the table) for persistent fields (in Java class).
We have 2 constructor methods:
– protected constructor will be used by Spring JPA.
– public constructor is for creating instances.
4. Create Spring JPA Repository Interface
This interface helps us do all CRUD functions for class Customer.
If we the query method is executed asynchronously, just annotate it with @Async annotation and return a kind of Future object:
CustomerRepository.java
package com.javasampleapproach.jpaasync.repo; import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.concurrent.Future; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.CrudRepository; import org.springframework.scheduling.annotation.Async; import com.javasampleapproach.jpaasync.model.Customer; public interface CustomerRepository extends CrudRepository{ @Async Future > findByLastName(String lastName); @Async CompletableFuture
findOneById(Long id); @Async @Query("select c from Customer c") CompletableFuture > findAllCustomers(); }
5. Create Web Controller
The controller receives requests from client, using repository to update/get data and return results.
Content of WebController.java
package com.javasampleapproach.jpaasync.controller; import java.util.Collections; import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.javasampleapproach.jpaasync.model.Customer; import com.javasampleapproach.jpaasync.repo.CustomerRepository; @RestController public class WebController { @Autowired CustomerRepository repository; @RequestMapping("/save") public String process() { repository.save(new Customer("Jack", "Smith")); repository.save(new Customer("Adam", "Johnson")); repository.save(new Customer("Kim", "Smith")); repository.save(new Customer("David", "Williams")); repository.save(new Customer("Peter", "Davis")); return "Done"; } @RequestMapping("/findall") public String findAll() { Future> future = repository.findAllCustomers(); // other tasks... // work with future here... List
customers = Collections.emptyList(); try { customers = future.get(); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } return customers.toString(); } @RequestMapping("/findonebyid") public String findOneById(@RequestParam("id") Long id) { String result = ""; CompletableFuture future = repository.findOneById(id); // other tasks... // work with future here... try { Customer customer = future.get(); result = customer.toString(); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } return result; } @RequestMapping("/findbylastname") public String fetchDataByLastName(@RequestParam("lastname") String lastName) { Future > future = repository.findByLastName(lastName); // other tasks... // work with future here... List
customers = Collections.emptyList(); try { customers = future.get(); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } return customers.toString(); } }
For the methods which are annotated by @RequestMapping, we have used some methods of autowired repository which are implemented interface CrudRepository:
Future> findByLastName(String lastName); CompletableFuture
findOneById(Long id); CompletableFuture > findAllCustomers();
6. Create PostGreSQL table
Open pdAdmin III, use SQL Editor and make a query to create customer table:
CREATE TABLE customer( id BIGINT PRIMARY KEY NOT NULL, firstname VARCHAR(100), lastname VARCHAR(100) );
7. Run Spring Boot Application & Enjoy Result
– Config maven build:
clean install
– Run project with mode Spring Boot App
– Check results:
Request 1
http://localhost:8080/save
The browser returns Done
and if checking database testdb with table customer, we can see some data rows has been added:
Request 2
http://localhost:8080/findall
[Customer[id=1, firstName='Jack', lastName='Smith'], Customer[id=2, firstName='Adam', lastName='Johnson'], Customer[id=3, firstName='Kim', lastName='Smith'], Customer[id=4, firstName='David', lastName='Williams'], Customer[id=5, firstName='Peter', lastName='Davis']]
Request 3
http://localhost:8080/findonebyid?id=2
Customer[id=2, firstName='Adam', lastName='Johnson']
Request 4
http://localhost:8080/findbylastname?lastname=Smith
[Customer[id=1, firstName='Jack', lastName='Smith'], Customer[id=3, firstName='Kim', lastName='Smith']]