Today i am going to explain how to develop simple CRUD (Create,Retrieve,Update and Delete data) application with Spring Boot and Spring Data JPA. the application will be simple web application and which will expose few REST endpoints for CRUD related operations.
What is Spring Data JPA?
Spring Data JPA is a sub project that comes under the Spring Data project and it is under the umbrella/shelter of Spring family. This will implements the JPA (Java Persistence Api) specification and have classes and methods for accessing and managing the data tier of the application. It tries to simplify the complexity associated with the data access layer of the application. As i have already mentioned, Spring Data JPA has following objectives.
- provides a better implementation for the JPA Specification.
- avoid the complexity associated with the Data Access layer of the application (by providing a framework that is easy to implement and having robust features)
So it is enough with theories. lets explore and play with the framework. As usual. i have used the https://start.spring.io/ for generating the project.
As you can see in the generation wizard, we are using two dependencies other than web dependency. That is JPA and MySQL.
JPA – Represents the maven dependency for Spring Data JPA.
MySQL – Represents the maven dependency for mysql-jdbc-connector
So Lets Open the pom.xml file and look at the dependency section for identifying those dependencies.
What we are going to build?
We will do CRUD related operations with a user object and ultimately we will build a REST api application with four endpoints for CRUD related operations.
- HTTP POST /api/users For creating the user
- HTTP PUT /api/users For updating the user
- HTTP DELETE /api/users/:user_id For deleting the user
- HTTP GET /api/users For retrieving all the users
Where do we start?
Spring Data JPA is all about creating Entities and Repositories.
Entity lives in between java object (java class) and relational table. It helps to map the java object to relational table and relational table back to java object.
Repository helps to perform CRUD related operations with Entity.
The actual definition of Entity and Repository might be different from the explanation that i have given. Basically i just wanted to simply point out the exact purpose/role of Entity and Repository.
So Lets create our Entity and Repository.
Creating the Entity
The Entity class will be the following class.
Things to Remember
The most important thing that you should note here is that we have used java persistence api (JPA annotations) based annotation here. No any framework specific annotation used. This is to compatible with other JPA implemented/supported ORM frameworks. For instance, if you have developed the data layer to compliant/adherent with JPA specification, then it will be possible to switch with different JPA implementations with minimal effort.
Creating the Repository
Creating a JPA based repository is very simple. You have to create an interface that extends the JpaRepository interface by providing the generic parameter, the Entity class and ID type. Here is the sample code for implementing UserRepository.
User – is the entity class
Long – is the data type of the ID (Primary Key).
All the methods required for the CRUD related operations will be inherited from the super level/parent interfaces. Therefore we do not have to explicitly add or implement CRUD related methods.
Now we have created both Entity class and Repository. Next step is to configure/provide the mysql connection details. It can be provided in the application.properties file.
This will try to connect to the database “spring_examples_db” running on local mysql server “localhost” on port “3306” by providing the username “root” and password “test123“. You should change these properties based on your mysql server details.
Now we should implement our REST endpoints for creating, updating, removing and listing users etc…. Please refer the below UserController.
Creating the User
Here is the sample postman request and response.
public User create(@RequestBody User user)
@RequestBody – The body of the HTTP request will be mapped to the User class.
@ResponseBody – The retuned object will be mapped to the body of the HTTP response.
Retrieving all the users
Retrieving a specific User/Resource
Updating a Specific User/Resource
Removing a Specific User/Resource
Once the requested resource is successfully removed, it will return a list of available users.
Ok. we have completed all the REST endpoints for managing users with Spring Data JPA.
Finally we will look at the Spring Boot main configuration class.
The configuration class is very simple and we have fully utilized the advantages of spring boot and no additional configurations have been made. We have taken fully advantage of spring boot auto configuration feature.
Why didn’t we use @EntityScan or @EnableJpaRepositories annotations here?
The package that contains the main Spring Boot Application configuration class (that is annotated with @SpringBootApplication) is known as the main application package.
If the entity classes and repository classes are placed under the spring boot main application package or its sub package, the spring boot will take care of detecting and scanning of entities and repositories. In such case wed do not have to explicitly declare the packages of Entities and Repositories with @EntityScan and @EnableJpaRepositories annotations respectively.
Please refer the below project structure.
If you look at the project file structure, you can notice that entity and repository packages are sub packages of the main application package. Therefore the entities and repositories placed under those packages will be automatically scanned and detected when the spring boot application is loaded.
If you want to know more about @EntityScan and @EnableJpaRepositories annotations, please go my article publish on that.
The full source code of this article can be found at GitHub.
Click here to Download The Source Code.
Build and Running the application
Build the application
mvn clean install -DskipTests
Run The application
java -jar target/spring-boot-data-jpa-example-0.0.1-SNAPSHOT.jar
Hope this article is helpful for you.