JPA EntityManagerFactory Interface With Example

DZone 's Guide to

JPA EntityManagerFactory Interface With Example

You can support instantiation of EntityManager instances in Java with this interface!

· Java Zone ·
Free Resource

The main role of an EntityManagerFactory instance is to support instantiation of EntityManager instances. An EntityManagerFactory is constructed for a specific database, and by managing resources efficiently (e.g. a pool of sockets), it provides an efficient way to construct multiple EntityManager instances for that database.

The instantiation of the EntityManagerFactory itself might be less efficient, but it is a one-time operation. Once constructed, it can serve the entire application. When the application has finished using the entity manager factory, and/or at application shutdown, the application should close the entity manager factory. Once an EntityManagerFactory has been closed, all its entity managers are considered to be in the closed state.

EntityManagerFactory Interface — Class Diagram

This class diagram shows a list of APIs that the EntityManagerFactory Interface provides.

EntityManagerFactory Interface — Method Summary

Here the list of important methods EntityManagerFactory interface for your reference:

  • void addNamedEntityGraph(String graphName, EntityGraph entityGraph) — This method add a named copy of the EntityGraph to the EntityManagerFactory.
  • void addNamedQuery(String name, Query query) — This method is used to define the query, typed query, or stored procedure query as a named query such that future query objects can be created from it using the createNamedQuery or createNamedStoredProcedureQuery method.
  • void close() — This method close the factory, releasing any resources that it holds.
  • EntityManager createEntityManager() — This creates a new application-managed EntityManager.
  • EntityManager createEntityManager(Map map) — This method is used to create a new application-managed EntityManager with the specified Map of properties.
  • EntityManager createEntityManager(SynchronizationType synchronizationType) — This method is used to create a new JTA application-managed EntityManager with the specified synchronization type.
  • EntityManager createEntityManager(SynchronizationType synchronizationType, Map map) — This method used to create a new JTA application-managed EntityManager with the specified synchronization type and map of properties.
  • Cache getCache() — Access the cache that is associated with the entity manager factory (the "second level cache").
  • CriteriaBuilder getCriteriaBuilder() — Return an instance of CriteriaBuilder for the creation of CriteriaQuery objects.
  • Metamodel getMetamodel() — Return an instance of the Metamodel interface for access to the metamodel of the persistence unit.
  • PersistenceUnitUtil getPersistenceUnitUtil() This method return the interface, providing access to utility methods for the persistence unit.
  • Map<String,Object> getProperties() — This method get the properties and associated values that are in effect for the entity manager factory.
  • boolean isOpen() — Indicates whether the factory is open.
  • T unwrap(Class cls) Return an object of the specified type to allow access to the provider-specific API.

EntityManagerFactory Interface Example

Let's demonstrates the important methods of the  EntityManagerFactory Interface with an example. In this example, we will use the createEntityManager() method to create a new application-managed EntityManager.

Step 1: Creating an Entity Manager Factory Object

The  EntityManagerFactory interface present in the java.persistence package is used to provide an entity manager.

EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("PERSISTENCE");

  • Persistence — The  Persistence is a bootstrap class, which is used to obtain an EntityManagerFactory interface.
  • createEntityManagerFactory() method — The role of this method is to create and return an EntityManagerFactory for the named persistence unit. Thus, this method contains the name of the persistence unit passed in the Persistence.xml file.

Step 2: Obtaining an Entity Manager From a Factory

EntityManager entityManager = entityManagerFactory.createEntityManager();

  • EntityManager — An EntityManager is an interface.
  •  createEntityManager() method — It creates a new application-managed EntityManager

Step 3: Intializing an Entity Manager


  • getTransaction() method — This method returns the resource-level EntityTransaction object.
  • begin() method — This method is used to start the transaction.

Step 4: Persisting a Data Into the Relational Database


  • persist() — This method is used to make an instance managed and persistent. An entity instance is passed within this method.

Step 5: Closing the Transaction


Step 6: Releasing the Factory Resources


  • close() — This method is used to release the factory resources.

Complete Example

private static void insertEntity() {
    EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("PERSISTENCE");
    EntityManager entityManager = entityManagerFactory.createEntityManager();

    Student student = new Student("Ramesh", "Fadatare", "rameshfadatare@javaguides.com");

A good resource to learn JPA at JPA Tutorial - Java Persistence API

Further Learning:


java ,jpa ,java persistence api ,entitymanagerfactory ,class ,tutorial ,entity ,entity manager

Published at DZone with permission of Ramesh Fadatare . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}