📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 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 (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
EntityManagerFactory Interface Overview
EntityManagerFactory Interface - Class Diagram
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
Step 1: Creating an entity manager factory object
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();
}
Comments
Post a Comment
Leave Comment