Does the EntityManager find () method create a new instance of the JPA class?

I'm a little confused. The question is in the title, and that is why I ask. I have a JSF + JPA web application running on the same virtual machine. And the JPA class has an @Transient field. Now imagine that some web user opens some page and executes the code below

 import javax.persistence.EntityManager; // method 1 in backing bean Agent a = entityManager.find(Agent.class, "007"); a.setTransientValue("Aston Martin"); 

What result should be expected when another web user / stream tries to read this transient value:

 // method 2 in backing bean Agent a = entityManager.find(Agent.class, "007"); String val = a.getTransientValue(); 

In other words, and in terms of the JVM, the find() method always returns a new instance of the class, or the same or "does it depend"? I scanned the JSR-220 for an answer, without success, any help or link to the doc would be appreciated.

+7
source share
2 answers

If you call find(..) in the same session (that is, during the same lifetime of the entitymanager), then the same object reference is returned. The documentation for find() states the following:

If an instance of an object is contained in a persistence context, it returns from there.

In other words, the EntityManager contains a set (most likely a map) of objects. When you call find , it checks this collection. If the object is not found there, a request is made to the database. The returned object is placed on the map, so subsequent calls will find it there.

But note that this is only for the range of one session. This is usually the same HTTP request (in the context of a web application)

+7
source

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.

+5
source

All Articles