Configuring a Hibernate 5 Application with JPA Entity Managers

In JPA, the EntityManager controls the lifecycle of the entities.

Different ORM vendors provide its own entity manager, which is usually a wrapper around the core API and thus supports the JPA programming interfaces, life cycles and query language.

You obtain the EntityManager from an entity manager factory. EntityManagerFactory has the same role as SessionFactory in Hibernate.

When entity managers are managed by a container, the application doesn’t interact directly with the entity manager factory, but the container does. Such entity managers are obtained mostly through JNDI lookup. In the case your application is managing the entity managers, the application must use the entity manager factory to manage the entity manager and the persistence context life cycle.

According to JPA specification, a persistence context is a set of managed entity instances.  Entities are thus managed within a persistence context.

If you are using maven, entity manager dependency can be added using the following dependency in the pom.xml file:







Configuring entity manager

Entity manager can be configured either programmatically or by using XML.

XML based configure is done through a file called persistence.xml and must be located in the classpath.

The persistence.xml files should provide a unique name for each persistence unit. This unique name can be used to refer to the configuration while obtaining a javax.persistence.EntityManagerFactory reference.


Sample persistence .xml file

<persistence xmlns=""



xml/ns/persistence/persistence_2_0.xsd" version="2.0">

<persistence-unit name="sampleunit" transaction-type="RESOURCE_LOCAL">


<mapping-file>com /Sample1.hbm.xml</mapping-file>

<mapping-file>com /Sample2.hbm.xml</mapping-file>





<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>

<property name="javax.persistence.jdbc.user" value="test"/>

<property name="javax.persistence.jdbc.password" value="test"/>

<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306"/>

<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>

<property name="" value="create"/>

<property name="hibernate.show_sql" value="true"/>




Imp Note! The persistance.xml should be present in a folder META-INF in your classpath. Either create a jar as such (jar -cf pers.jar *) or add this folder to your class path (if in eclipse, create under src folder).

Transactional behaviour is specified through transaction-type attribute of the persistence-unit element. It can have two values: JTA and RESOURCE_LOCAL.JTA is used in container managed applications in which the container is responsible for transaction propagation. For application managed transactions, we can use RESOURCE_LOCAL.

The <provider> tag can be used to specify the actual third-party ORM implementation you use, like Hibernate persistence provider.

The entity instances are configured with the <class> tag.

Properties are similar to the configuration for hibernate without JPA: For hibernate configuration, please refer to


Sample code: Creating the EntityManagerFactory and obtaining the EntityManager

public static EntityManager getEntityManager() {

EntityManagerFactory managerFactory =

Persistence.createEntityManagerFactory("sampleunit ");

EntityManager manager = managerFactory.createEntityManager();

return manager;



The test class will have code that look similar to below:

EntityManager em = emf.createEntityManager();


User u = new User();





Note: To retrieve the book details from the database, use em.find(User.class, id).


Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) Arrays (1) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (55) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (5) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (9) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (7) Javascript Frameworks (1) Java SE Professional (1) JPA 1 - Module (6) JPA 1 - Modules (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)