EntityManager的find()方法是否创建JPA类的新实例? [英] Does EntityManager's find() method create new instance of JPA class?
问题描述
我的JSF + JPA Web应用程序在单个虚拟机上运行。
一个JPA类有
@Transient
字段。现在想象一些网页用户打开一些页面并执行下面的代码 import javax.persistence.EntityManager;
//方法1在支持bean
代理a = entityManager.find(Agent.class,007);
a.setTransientValue(Aston Martin);
当另一个Web用户/线程尝试读取该临时值时,应该输出什么输出:
//方法2在备份bean
代理a = entityManager.find(Agent.class,007);
String val = a.getTransientValue();
换句话说,就JVM而言, find() / code>方法返回总是新的类实例或相同或依赖?我看过JSR-220的答案,没有成功,任何帮助或文档参考将不胜感激。
如果您在同一个会话内调用 如果实体实例包含在持久性上下文中,则从该对象返回。 换句话说, 但是请注意,这仅仅是一个会话的范围。这通常与一个http请求(在Web应用程序上下文中)相同。 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 What output should I expect when another web user/thread tries to read that transient value: In other words, and in terms of JVM, does If you invoke If the entity instance is contained in the persistence context, it is returned from there. In other words, the 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) 这篇关于EntityManager的find()方法是否创建JPA类的新实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! find(..)
(即在同一个entitymanager生存期内),则返回相同的对象引用。
EntityManager
持有一个集合(最有可能的映射)的实体。当你调用 find
它会检查该集合。如果没有找到该实体,则会对数据库进行查询。返回的实体被放入地图中,所以随后的调用将在那里找到。
@Transient
field. Now imagine some web user opens some page and executes code belowimport javax.persistence.EntityManager;
// method 1 in backing bean
Agent a = entityManager.find(Agent.class, "007");
a.setTransientValue("Aston Martin");
// method 2 in backing bean
Agent a = entityManager.find(Agent.class, "007");
String val = a.getTransientValue();
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.find(..)
within the same session (that is, within the same entitymanager lifetime), then the same object reference will be returned. The documentation of find()
specifies this:
EntityManager
holds a collection (map most likely) of entities. When you call find
it 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.