Using JPA in Spring MVC

I have shown you how to use JPA in the Spring framework in the previous tutorial. In this tutorial, I will continue to guide you all using JPA in Spring MVC.

First, I will also create a Maven project as an example:

Using JPA in Spring MVC

If someone does not know how to create, just follow the instructions of this tutorial.

The default dependencies have changed version as follows:

To work with JPA, we need to add spring-orm dependency:

To work with JPA with Hibernate implementation, I also need to add Hibernate’s dependency as follows:

MySQL Connector:

I will use Project Lombok to avoid having to declare Getter or Setter methods.

To run this project, I will use Maven Jetty Plugin:

In this example, I will create a small web application that allows users to query a student’s information.

We will organize the project model as: from Controller, want to manipulate the database, must through the Service class. The Service Class will act as an intermediary and database related things will not be used directly in the Controller.

To do this, we need to perform the following steps in turn:

First of all, we need to define a student database.

For the sake of simplicity, I will define a table containing student information with two columns:

Next, I will define the Student entity to represent the information of the student table.

In this entity, I will use Project Lombok’s @Data annotation to avoid declaring Getter, Setter, toString(), equals(), or hashCode() methods.

To work with JPA, similar to the Spring framework, we also need a config file persistence.xml for it.

Let’s create a new file called persistence.xml located in /src/main/resources/META-INF.

The contents of this file are as follows:

Now we are going to declare EntityManagerFactory in the Spring container.

As in the Spring framework, I also use the LocalContainerEntityManagerFactory and declare it as follows in /src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml:

We will use the StudentDao interface and its implementation StudentDaoImpl to manipulate the database directly.

StudentDao interface’s content is as follows:

Class StudentDaoImpl implement StudentDao interface, that will be declared with the @Repository annotation (used to declare this Spring bean will work with the database). The content of this class is as follows:

In this class, I have injected the EntityManager object from the Spring container with the @PersistenceContext annotation. And we will use this EntityManager object to find students by ID.

For the controller to work with the database, I will create a new StudentService interface and its StudentServiceImpl implementation for the Controller to use.

The StudentService interface has the following contents:

And class implementation StudentServiceImpl:

Here, I have declared the StudentServiceImpl class using the @Service annotation, and I also injected the StudentDao bean into the StudentServiceImpl object to manipulate the database.

Here, I also use another Student object, used only in the Service level and Controller. The separation between the Student object for the database and the Student object for the Controller will make it easy to edit later.

The contents of class com.huongdanjava.springmvcjpa.web.entity.Student is as follows:

Now we will use the StudentService bean in the Controller so that it can use this object to manipulate with the database.

I will edit the class HomeController using StudentService to find student information by ID as follows:

After the results, I will put it into the Model with the key “student” to display on the View as follows:

So we have completed the necessary steps. Now run up!

Assuming your database is having data like this:

Using JPA in Spring MVC

then the result will be as follows:

Using JPA in Spring MVC

 

Add Comment