Configuring a Hibernate 4.3 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).


Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within!!!