I’m a bit confused. The question is in title, and here’s why I’m asking.
I have JSF + JPA web-application running on a single VM.
And an JPA class has @Transient field. Now imagine some web user opens some page and executes code below
import javax.persistence.EntityManager;
// method 1 in backing bean
Agent a = entityManager.find(Agent.class, "007");
a.setTransientValue("Aston Martin");
What output should I expect when another web user/thread tries to read that 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 JVM, does find() method return always new class instance or the same or ‘it depends’? I’ve looked through JSR-220 for an answer, with no success, any help or doc reference would be appreciated.
If you invoke
find(..)within the same session (that is, within the same entitymanager lifetime), then the same object reference will be returned. The documentation offind()specifies this:In other words, the
EntityManagerholds a collection (map most likely) of entities. When you callfindit checks that collection. If the entity is not found there, a query to the database is made. The returned entity is put into the map, so subsequent calls will find it there.But note again that this is only for the span of one session. This is usually the same as one http request (in the web app context)