🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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");
- Persistence - Persistence 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.
entityManager.getTransaction().begin();
- 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.
entityManager.persist(student);
- 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
entityManager.getTransaction().commit();
Step 6: Releasing the factory resources.
entityManager.close();
entityManagerFactory.close();
- 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();
entityManager.getTransaction().begin();
Student student = new Student("Ramesh", "Fadatare", "rameshfadatare@javaguides.com");
entityManager.persist(student);
entityManager.getTransaction().commit();
entityManager.close();
entityManagerFactory.close();
}
Related Posts
Reference
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
🆕 High-Demand
80–90% OFF
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
🆕 High-Demand
80–90% OFF
ChatGPT + Generative AI + Prompt Engineering for Beginners
🚀 Trending Now
80–90% OFF
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
🌟 Top Rated
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
🌟 Top Rated
80–90% OFF
Testing Spring Boot Application with JUnit and Mockito
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Master Spring Data JPA with Hibernate
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
🎓 Student Favorite
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business

Comments
Post a Comment
Leave Comment