JPA EntityManagerFactory Interface with Example

EntityManagerFactory Interface Overview

The main role of an EntityManagerFactory instance is to support the instantiation of EntityManager instances. 

An EntityManagerFactory is constructed for a specific database, and by managing resources efficiently (e.g. a pool of sockets), 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 EntityManagerFactory Interface provides.

EntityManagerFactory Interface - Method Summary

  • void addNamedEntityGraph(String graphName, EntityGraph entityGraph) - Add a named copy of the EntityGraph to the EntityManagerFactory.
  • void addNamedQuery(String name, Query query) - 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() - Close the factory, releasing any resources that it holds.
  • EntityManager createEntityManager() - Create a new application-managed EntityManager.
  • EntityManager createEntityManager(Map map) - Create a new application-managed EntityManager with the specified Map of properties.
  • EntityManager createEntityManager(SynchronizationType synchronizationType) - Create a new JTA application-managed EntityManager with the specified synchronization type.
  • EntityManager createEntityManager(SynchronizationType synchronizationType, Map map) - 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() - Return interface providing access to utility methods for the persistence unit.
  • Map<String,Object> getProperties() - 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 demonstrate the important methods of EntityManagerFactory Interface with an example. In this example, we will use createEntityManager() method to create a new application-managed EntityManager.

Step 1: Creating an entity manager factory object

The EntityManagerFactory interface is present in java.persistence package is used to provide an entity manager.
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("PERSISTENCE");
  • PersistencePersistence is a bootstrap class that 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 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 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", "[email protected]");

Related Posts