To understand how this works, it is important to understand the relationship between the entity manager and the context.
An object manager is an open interface through which you access your entities, however your entities are in a context attached to your entity manager. Understanding the life cycle of various types of contexts will answer your question.
Saving contexts can be of different types. In Java EE applications, you can either have a save transaction context context or an extended save context . In a JSE application, the nature of the context is controlled by the developer .
When you request an entity for your entity manager, it searches for the object in its nested context, if it finds the object there, then it returns it, otherwise it retrieves the object from the database. Subsequent calls to this object in the context return the same object.
visibility deal
In a Java EE application that uses a transactional persistence context, when you first access your entity manager, it checks if the current JTA transaction has an attached context, if the context does not already exist, a new context is created and the object manager is associated with that context. Then the object is read from the database (o from the cache, if present), and it is placed in context. When your transaction ends (commit or rollback), the context becomes invalid and any objects in it become detached. This is a classic stateless session script for beans.
@PersistenceContext(unitName="EmployeeService") EntityManager em;
It also means that depending on how you design your transactions, you may encounter several contexts.
Enhanced Persistence Context
In a Java EE application with a statefull beans session, you might need the context to withstand multiple bean calls, as you will not like the commit until the bean is marked for deletion, right? In these cases, you need to use the extended save context. In this case, the persistence context is created when it is first needed, but it will not become invalid until your token removes the bean state.
@PersistenceContext(unitName="EmployeeService", type=PersistenceContextType.EXTENDED)
This means that regardless of the object manager instance that is introduced into this bean in subsequent calls to statefull session beans methods, you can be sure that you will always access the same context, even subsequent calls will return the same instance, therefore that is the same context.
In addition, your changes will not be cleared until the bean is marked for deletion or you manually clear them.
Managed Application
You can always manually create a factory entity manager manually and your own entity manager. This is what you usually do in a JSE application, right?
For such applications, you usually do not have a container for processing JTA transactions, right? This way you use local resource transactions, and you are responsible for manually committing or rolling back the changes.
For this type of application, when you create an instance of an entity manager, the context is automatically bound to it.
Depending on your application, you can create a global entity manager whose life cycle is related to the life of the application itself. That is, one entity manager for the entire life of the application. In this case, your context will be created and destroyed by your entity manager.
Or you could create an entity manager to talk (i.e. transaction) with your application user. The scope in this case is determined by you, but nonetheless, your context will be created and destroyed by your entity manager.