How to execute asynchronous query with Spring JPA and PostgreSQL | Spring Boot

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.
springjpa-postgresql-configdependencystarter
Click Finish, then our project will be created successfully.

Open pom.xml and check Dependencies:

These dependencies were auto-generated by the configuration we have done before.

2. Configure Spring JPA

Open application.properties

3. Create DataModel Class

Under package model, create class Customer.

Content of Customer.java:

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

5. Create Web Controller

The controller receives requests from client, using repository to update/get data and return results.

Content of WebController.java

For the methods which are annotated by @RequestMapping, we have used some methods of autowired repository which are implemented interface CrudRepository:

6. Create PostGreSQL table

Open pdAdmin III, use SQL Editor and make a query to create customer table:

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

Request 3
http://localhost:8080/findonebyid?id=2

Request 4
http://localhost:8080/findbylastname?lastname=Smith

IV. Source Code

SpringJPAAsyncQuery


Related Posts



Got Something To Say:

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

*