JPA and Spring framework

In JPA, the EntityManagerFactory object is an entity management object that manages the connection to the database. Using Spring to manage this object will help us a lot in developing Java applications that use JPA. In this tutorial, I will guide you on how to use JPA with the Spring framework.

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

JPA and Spring framework

Spring framework dependency:

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

Here, I will use JPA with the implementation of Hibernate so I will add Hibernate dependency as follows:

MySQL Connector:

For simplicity, in this example, I only define a table containing student information with two columns:

The entity of student table has the following contents:

To work with JPA, we need a persistence.xml configuration file 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, the most important part is: to declare the EntityManagerFactory object in the Spring container.

In Spring 5, we currently have two classes that support declaring the EntityManagerFactory object:

  • LocalContainerEntityManagerFactoryBean
  • LocalEntityManagerFactoryBean

With the LocalEntityManagerFactoryBean class, the JPA persistence.xml configuration file is usually located in the META-INF/persistence.xml folder, but with the LocalContainerEntityManagerFactoryBean class, the location of the persistence.xml file is more flexible. We can override its location and using LocalContainerEntityManagerFactoryBean will help you be more flexible on the JDBC DataSources declaration.

I will use the LocalContainerEntityManagerFactoryBean in this example to declare it in the Spring container as follows:

As you can see, there are three properties that we need to declare with the LocalContainerEntityManagerFactory class:

  • dataSource: This property is used to declare the DataSource for our EntityManagerFactory. The DataSource here, we can understand, is the management of the connection to the database. We can declare many different types of DataSource as many different organizations implement the javax.sql.DataSource interface.
  • persistenceUnitName: This is the name of the Persistence Unit that we have declared in the JPA configuration file, persistence.xml.
  • jpaVendorAdapter: This attribute is used to declare the library we will use for the JPA implementation. In this example, I use Hibernate then I declared HibernateJpaVendorAdapter. If you use EclipseLink as a JPA implementation, just declare this property as org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter.

The dataSource section in this example, I will use DriverManagerDataSource with declarations as we do for JDBC, as follows:

OK, so we have completed the EntityManagerFactory declaration in the Spring container. Now use it!

My example is as follows:

My database currently has the following data:

JPA and Spring framework

So the result would be:

JPA and Spring framework

Add Comment