Spring Data JPA Introduction - Part 2
The second part of this series on Spring Data JPA goes through the CRUD operations with a user profile.
Join the DZone community and get the full member experience.Join For Free
We will discuss about the following with a small implementation for creating a new user.
An introduction to CREATE Operation.
- REST Controller (@RestController)
- Service class (@Service)
- Use Model classes.
- ResponseEntity to return status code, headers and body.
- Unique Constraints.
- Finder Methods.
- Optional Class.
I use the following:
- JDK 1.8
- Postman as my REST Client
Our Entity Model
The User Class
All we have to do here is to define the fields and write the getter and setter methods. Here, I have used the
@Column(unique=true). It sets an unique constraint on email, which is to say, duplicate entries of email will not be allowed.
User Repository Interface
Here we are using a finder method which takes in an email and returns an Optional<User>. This can be used in conjunction with the
isPresent() method to check whether it has returned an user. We don't have to check for null.
User Service Class
Here, we are defining a method
createUser that takes in model of type
What we do is we create the new User. We get the values from the model object and will assign it to the new user we have created. First, we will start by checking whether the email is already present. If it is present I am using the
ResponseEntity class to return "email is already present."
ResponseEntity has many handy methods, so that you can format your return types. You can control anything that goes into the response: status code, headers, and body. Here we are setting the body of the response.
After assigning the values to the new User, I am using the save method given by the
JpaRepository . The
Save method returns a User Object and I am assigning it to
savedUser. In the next line of code I check if it is present. It is to make doubly sure that the user has been saved. If it is present it will return that the user has been created successfully; otherwise I will get a failure message.
User Model Class
We use this model class for data binding. You can see how it is used in the controller.
Using Model Class is one way of doing things. It separates the presentation concerns and persistent concerns. It is not mandatory in all cases. In this I am using a model class so we get an understanding and awareness of how it can be used.
Here I am autowiring the service class with the
@Autowired annotation. We use the @PostMapping annotation for all the post request from the rest clients.
We will use the Request body to pass in the data of type
UserModel. This annotations are handy for binding requests with a domain object, in our case, the
Now let's run the application. First, open Postman.
My JSON object creates an User.
Once you click send, you can see that the user is created in the database.
Please find the source code here.
You can find the video tutorials below.
Opinions expressed by DZone contributors are their own.