EntityManager class is the main API in JPA. It is used to create new entities, manufacture queries to return
sets of existing entities, merge in the state of remotely modified entities, delete entities from the database,
The set of entities that can be managed by a given EntityManager instance is defined by a persistence unit.
To create an EntityManager, we first create an
EntityManagerFactory for the Persistence Unit.
EntityManagerFactory emf = Persistence .createEntityManagerFactory("HelloWorldPU");
The factory is thread-safe, and all code in your application that accesses the database should share it.
Creating a basic query
EntityManager em = emf.createEntityManager(); // begin transaction em.getTransaction().begin(); User user = new User(); user.setName("Naveen"); em.persist(user); // begin transaction em.getTransaction().commit();
We have our first entity object persisted in the database. Let's see what's happening here :
- First, we get an entity manager from the entity manager factory.
- Next, we get the entity manager's local transaction and begin it.
- We create a new User object, nothing new here just plain old java code.
em.persist(user)is where the magic happens. This makes the User object an Entity.
- And finally the commit statement completes the transaction and updates the database with our new user.
The Magic of JPA
The persist method is very interesting. A lot of things are happening under that single method call. The user object which was a plain old java object (POJO) is converted into an Entity.
The Entity Object is now being tracked by Hibernate. If you make any changes to it, Hibernate will automatically update the underlying database value too.
Now consider the implications of that statement and you will understand how much work the ORM does for us.
Manipulating domain model entities (along with their associations) is much easier than writing and maintaining SQL statements. We can programmatically modify the entities and Hibernate will automatically take care of updating the database.
persist method is not the only way get an Entity. Consider the following code for querying the list of users.
List<User> users = em.createQuery("select u from User u").getResultList();
The list of users that will be returned are also entities so to speak. They are being actively managed by Hibernate. Updates to them within the transaction will cause changes to the underlying database. How cool is that ?
A transaction is not required when you are only reading data.
A persistence context is like a cache which contains a set of persistent entities , So once the transaction is finished, all persistent objects are detached from the EntityManager's persistence context and are no longer managed.
- Entities are managed by javax.persistence.EntityManager instance using persistence context.
- Each EntityManager instance is associated with a persistence context.
- Within the persistence context, the entity instances and their lifecycle are managed.
- Persistence context defines a scope under which particular entity instances are created, persisted, and removed.
Things to Remember
- It must be a top-level class, not nested within another class.
- The persistence-capable class and any of its methods can’t be final.
- Hibernate (and JPA ) requires a constructor with no arguments for every persistent class.
- Hibernate can either directly access fields or call accessor methods. The choice is up to us.
- Collections are compared by identity. For a property mapped as a persistent collection, you should return exactly the same collection instance from the getter method that Hibernate passed to the setter method.