JPA EntityManager Example

In this example, we shall try to demonstrate how to use JPA EntityManager. As the name suggests, an EntityManager is a class that manages the state of the Entity(Persist/Update/Delete etc).

Every EntityManager object has an instance of EntityTransaction associated with it. EntityTransaction is used to manage the transactions.

We shall be using Hibernate as the JPA Vendor. The underlying database shall be MySQL.

The benefit of using the JPA over any specific ORM related libraries like Hibernate, iBatis is that we need not change the code when we change the vendor. The code is decoupled (or loosely coupled) with the underlying ORM framework.

Let’s build a sample application to see how we can avoid using Hibernate specific interfaces and use JPA interfaces instead:

package com.jcg.pojo;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

public class Employee
		protected Long employeeId;
		protected String name;

		public  Long getEmployeeId()
				return employeeId;

		public  void setEmployeeId(Long employeeId)
				this.employeeId = employeeId;

		public  String getName()
				return name;

		public  void setName(String name)
		{ = name;

    public String toString()
		    return "Employee [employeeId=" + employeeId + ", name=" + name + "]";

package com.jcg;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import com.isg.maps.Employee;

 * @author Chandan Singh
public class JPADemo
		public static void main(String[] args)
				EntityManagerFactory emf = Persistence.createEntityManagerFactory("jcg-JPA");
				EntityManager em = emf.createEntityManager();
				Employee employee = new Employee();

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns=""
        <persistence-unit name="jcg-JPA">
			<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
			<property name="" value="update" />
			<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
			<property name="hibernate.connection.username" value="myusername" />
			<property name="hibernate.connection.password" value="mypwd" />
			<property name="hibernate.connection.url" value="jdbc:mysql://localhost/research_development" />
  1. We define the connection properties in the persistence.xml.
  2. Then we look up the persistence unit from the createEntityManagerFactory method of Persistence class of JPA. This returns an object of EntityManagerFactory.
  3. Finally, we can get the EntityManager object from the EntityManagerFactory class.
  4. We ,now, use the EntityManager object to perform CRUD operation on the Entities under the scope of an EntityTransaction object.
  5. The last step is to commit the transaction back tot the database.
It is mandatory to place persistence.xml in the META-INF folder.


Thus we studied about the JPA EntityManager and how we can use it to avoid dependency on any particular ORM Framework.

You can download the source code of this example here:

Chandan Singh

Chandan holds a degree in Computer Engineering and is a passionate software programmer. He has good experience in Java/J2EE Web-Application development for Banking and E-Commerce Domains.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
Purvesh Dwivedi
Purvesh Dwivedi
3 years ago literally??

Back to top button