Updated April 1, 2023
Introduction to Hibernate EntityManager
Hibernate EntityManager or, also called JPA EntityManager has an important aspect of connecting with the database of a program. One of the most expensive transactions is considered to be the Database connection. In terms of java objects, the ORM helps in the representation of database relations. Relational programming and Oops are the two concepts of ORM. A programmer gives a description for the way the objects are to be represented in a database in ORM framework – Hibernate. EntityManager and EntityManagerFactory are teJPA interfaces which Hibernate provides in implementation. Instances are provided for connecting the same database by EntityManagerFactory.
Syntax:
From EnttityManagerFactory, EntityManager is created.
@PersistenceContext
EntityManager entityManager;
The container is in-charge of transaction in the beginning and the rolling back as well.
How Hibernate EntityManager Annotation Work?
All instances have the same configuration so as to use the same setting defined by implementation default. Many factories of entity managers can be prepared for connection of different datastores. EntityManager JPA is used in accessing databases in any particular application.
Also used in managing persistence instances of entities, find entities by identity primary key, and also query the overall entities.
persist | Instance is managed and persisted. |
merge | The state of a given entity is merged into the current context of persistence. |
remove | All entity instances are removed. |
flush | Synchronize database with persistence context. |
getReference | Entity Not Found exception is thrown. This happens when the instance happens for the first time. |
find | Ab entity is searched which specifies class and primary key. If found, returned. |
setFlushMode | For all the objects of persistence context, set flush mode. |
lock | With the specified lock mode type, lock the entirety instance from the database. |
refresh | Overwriting and refreshing the state of instances. |
clear | Persistence context is cleared. All managed entries are detached. Entities that have not been flushed and are changed, are not persisted. |
detach | Familiar to method clear. Detached objects will be referenced. |
contains | Looks over if the entity belongs to current persistence context. |
getLockMode | Current lock mode for instance. |
setProperty | Entity manager property set. |
getProperties | Hints and properties with the entity manager. |
createNativeQuery | Instance query is created for execution of native sql statement language. |
createNamedQuery | Instance of query is created four Java Persistence execution. |
createQuery | Instance of query is created for the execution of Java persistence query language statements. |
createNamedStoredProcedureQuery | Instance is created for execution of a stored procedure in a database. |
createStoredProcedureQuery | Instance is created for execution of a stored procedure in a database. |
joinTransaction | Indicates that JTA transaction is active to the entity manager. |
isJoinedToTransaction | Tells if the entity manager links to the current transaction. |
unwrap | Access is allowed to the provider specific API and object is returned. |
getDelegate | Provider object is returned for entity manager and returned. |
close | Application is closed by the entity manager. |
isOpen | Look if the entity manager is open. |
getTransaction | Resource level entity transaction object is returned. |
getEntityManagerFactory | Entity manager is provided with an entity manager factory. |
getCriteriaBuilder | For creation of criteria query objects, criteria builder is returned. |
getMetamodel | Meta model of the persistence unit is accessed with the Meta model interface and is returned. |
createEntityGraph | A mutable entity graph is returned and used in a dynamically created entity graph. |
getEntityGraph | A named entity graph is returned. |
getFlushMode | For all the objects of persistence context, get flush mode. |
persist() is a method of EntityManager to manage the entity and persist the database. We should pass entity to save data.
Examples of Hibernate EntityManager
Below are the examples of Hibernate EntityManager:
Example #1
Code:
package com.concretepage;
import javax.persistence.EntityManager;
import com.concretepage.entity.Employee;
public class JPAWriteDemo {
public static void main(String[] args) {
EntityManager entityManager = JPAUtility.getEntityManager();
entityManager.getTransaction().begin();
Employee employee = new Employee(1, "Mahesh", "Varanasi");
entityManager.persist(employee);
entityManager.getTransaction().commit();
entityManager.close();
JPAUtility.close();
System.out.println("Entity saved.");
}
}
package com.democlass_entitymanager;
import javax.persistence.EntityManager;
import com.concretepage.entity.Employee;
public class JPASelectDemo {
public static void main(String[] args) {
EntityManager entityManager = JPAUtility.getEntityManager();
Employee employee1 = entityManager.find(Employee.class, new Integer(1));
System.out.println("Name:"+ emp.getName()+", City:"+ emp.getCity());
JPAUtility.close();
System.out.println("Done");
}
Output:
Example #2
Code:
package com.democlass_entitymanager;
import javax.persistence.EntityManager;
import com.concretepage.entity.Employee;
public class JPAUpdateDemo {
public static void main(String[] args) {
EntityManager entityManager = JPAUtility.getEntityManager();
Employee emp = entityManager.find(Employee.class, new Integer(1));
System.out.println("Name:"+ emp.getName()+", City:"+ emp.getCity());
entityManager.getTransaction().begin();
emp.setName("Krishna");
emp.setCity("Allahabad");
entityManager.getTransaction().commit();
emp = entityManager.find(Employee.class, new Integer(1));
entityManager.close();
JPAUtility.close();
System.out.println("Name:"+ emp.getName()+", City:"+ emp.getCity());
System.out.println("Entity updated.");
}
}
Output:
Conclusion
In this article, we learned about hibernate entityManager. The types of methods that can be used are studied which are available in the API. They work with persistence context.
Recommended Articles
This is a guide to Hibernate EntityManager. Here we discuss how Hibernate EntityManager Annotation Works and its Examples along with its Code Implementation. You can also go through our other suggested articles to learn more –