EntityManager

EntityManager provides the persistence context in which the entities exist. The persistence context also allows you to manage transactions. Using EntityManager APIs, you can perform query and write operations on entities. The entity manager can be web-container-managed (in which case an instance of EntityManager is injected by the container), or application-managed. In this chapter, we are going to look at application-managed entity managers. We will visit container-managed entity managers in Chapter 7, Creating JEE Applications with EJB, when we learn about EJBs. The persistence unit of the entity manager defines the database connectivity information and groups entities that become part of the persistence unit. It is defined in the configuration file called persistence.xml and is expected to be in META-INF in the class path.

EntityManager has its own persistence context, which is a cache of entities. Updates to entities are first done in the cache and then pushed to the database when a transaction is committed or when the data is explicitly pushed to the database.

When an application is managing EntityManager, it is advisable to have only one instance of EntityManager for a persistence unit.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.235.8