JPA basic example with EntityManager , Spring and Maven

In this post we are going to make a project that allows us to get from a mySql database a car list. The technologies we are going to use are Maven for the dependency management, JPA to interact with the database, Spring to define the our application context (besides of wiring everything), and finally we use JUnit to our application tests.

We have also a data access layer where there is a interface called CarDao that defines the methods that we use to interact with our database. This interface will be implemented by the CarDaoImpl.
In some of the later projects we have use a HibernateTemplate object to get the data from our database. Instead of that we are going to use an EntityManager instance. An EntityManager will be a instance of an EntityManagerFactory within JPA. This type of factory means the configuration used to access to our database.
To use JPA in our project we are going to create a persistence unit in the META-INF/persistence.xml file. Within this file we will have a specified persistence unit. What we should do is choosing the transaction type. We need to use the provider that will implement JPA. In our case, we are going to use Hibernate.

We need to configure our application context in the same way we did when we used an Hibernate sessionFactory. That means, we have to add a dataSource within our context file where we define our url , user, password and the database we are going to use. We define an entityManagerFactory to which we pass the newly created dataSource. This entityManagerFactory object will be controlled by a transactionManager. As we are using JPA we need a org.springframework.orm.jpa.JpaTransactionManager.
There is also a component-scan element. We will explain it when we see our Dao. Our context file is as follows:

There is no much to say about the carDao interface. Two methods that we implement within the CarDaoImpl class. We need to explain a couple of things.

First of all we notice that CarDaoImpl is annotated as @Repository. This is an annotation we use to say to the Spring context that our CarDaoImpl is included within its context as a component. Besides the @Repository annotation there are a @Service annotation and a @Controller one. Every annotation extends to the @Component annotation. There is a meaning difference among them, @Repository will indicate our Spring context that we want to add a Dao bean, @Controller a controller and @Service … easy to guess. Do you remenber thecomponent-scan element within our testApplicationContext file?, in this file we tell Spring we are going to incluide as beans every single class with the @Repository annotation, in our case, CarDaoImpl car.

The second annotation we notice is @PersistenceContext. It is in the EntityManager setter method. We get an EntiyManagerFactory object we need for address our database.

We can notice that our methods are annontated as transactional. That means our methods belong to a unique transaction that will be management by the transactionManager we explained in our context file.

Our method logic is quite straightforward. We have a couple of methods, the first one list every car we have stored in our database and the second one use an id to find a specified car. To that we have to use an entityManager. As with HibernateTemplate, our entityManager class will have some basic methods like persist, refresh ó remove. Once our application is created we are going to make a test class.

We annotate our class as always, pointing that we are going to use JUnit4 and the context file we have already created. We are going to get our CarDao from the Spring context. We have some method that we are going to test from a Maven test configuration within STS.

thank you for reading the post and apologies for answering this late. Recreate the project is very easy. One thing you can do is simply creating the skeleton of a maven app following a maven archetype. There are different types of archetype for everything. In this case I followed the webapp archetype. If you have maven installed in your laptop, you just need to run:

Good tuto, congrats. Tested and working for me, it obtains the full car list and finds a car by id. Problem comes for me when I want a Car to be saved: javax.persistence.TransactionRequiredException: no transaction is in progress. It happens to me everytime I call flush() on the EntityManager. Have a look at my test project in GitHub, there’s a test case with the issue: https://github.com/xtremebiker/jsf-spring-test