EntityManager.find() 和 EntityManger.getReference() 有什么区别?

发布于 2024-10-28 03:16:54 字数 483 浏览 4 评论 0原文

和 有什么区别

<T> T EntityManager.find(Class<T> entityClass, Object primaryKey) and 
<T> T EntityManager.getReference(Class<T> entityClass, Object primaryKey) 

我认为 getReference 返回实体(如果它是托管的)。 如果它是托管的,find 返回实体,否则在数据库上执行 SQL 以使其托管。

请确认。


语境: 从 webapp 我获取要删除的对象的主键(long 类型的 pk);实体应设法删除。

EntityManager.remove(Object entity)

将托管实体传递给实体管理器删除方法'什么是更好和正确的选项?查找或获取参考?

Whats is the difference between

<T> T EntityManager.find(Class<T> entityClass, Object primaryKey) and 
<T> T EntityManager.getReference(Class<T> entityClass, Object primaryKey) 

?

I think getReference returns entity if it is managed.
and find returns entity if it is managed else executes SQL on database to make it managed.

Please confirm.


Context:
From webapp I get primary key of object to be deleted (pk of type long); to entity should be managed to delete.

EntityManager.remove(Object entity)

to pass managed entity to entitymanager remove method 'whats the better and correct option? find or getReference?'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

¢好甜 2024-11-04 03:16:54

如您所知,JPA 有 EntityManager 的概念。在实体管理器中工作期间,一些对象会从数据库加载,可以进行修改,然后刷新到数据库。

find() 必须返回对象的初始化实例。如果它尚未加载到 EntityManager 中,则会从数据库中检索它。

如果实体之前尚未加载到 EntityManager 中,则允许 getReference() 返回代理而不是初始化实例。在此代理中,仅初始化主键属性。 可以在不访问数据库的情况下创建代理,因为唯一的初始化属性已提供给 getReference() 函数。

当实体 A 引用实体 B,并且您希望将 A 的 b 属性设置为 B,而不必从数据库加载 B 时,后者很有用。

只有引用B的其他属性时,代理才会被初始化。

JPA has the concept of an EntityManager, as you know. During your work in the entity manager some objects are loaded from the database, can be modified and afterwards flushed to the database.

find() has to return an initialized instance of your object. If it is not already loaded in the EntityManager, it is retrieved from the database.

getReference() is allowed to return a proxy instead of an initialized instance, if the entity has not been loaded in the EntityManager before. In this proxy, only the primary key attribute is initialized. Proxies can be created without hitting the database, because the only initialized attribute is already given to the getReference() function.

The latter is useful when you have an entity A referencing an entity B, and you want to set the b-attribute of A to B, without having to load B from the database.

Only if you reference other attributes of B, the proxy will be initialized.

孤独患者 2024-11-04 03:16:54

Beginning Java EE 6 Platform with GlassFish 3,在第135页提到了差异:“Finding By ID”

find() 如果找到实体,则返回该实体;如果没有找到,则返回空值。

MyEntity obj = em.find(MyEntity.class, id);
if(obj != null){
   // Process the object 
}

getReference() 适用于需要托管实体实例,但除了潜在的实体主键之外没有数据被访问的情况。

try {
    MyEntity obj = em.getReference(MyEntity.class, id);
    // Process the object
} catch (EntityNotFoundException e) {
    // Entity Not Found
}

The book Beginning Java EE 6 Platform with GlassFish 3, mention the differences in page 135: "Finding By ID"

find() if the entity is found, it is returned; if it is not found, a null value is returned.

MyEntity obj = em.find(MyEntity.class, id);
if(obj != null){
   // Process the object 
}

getReference() is intended for situations where a managed entity instance is needed, but no data, other than potentially the entity's primary key, being accessed.

try {
    MyEntity obj = em.getReference(MyEntity.class, id);
    // Process the object
} catch (EntityNotFoundException e) {
    // Entity Not Found
}
仙女 2024-11-04 03:16:54

getReference() 不会检索完整的对象,而只会检索代理,因此如果您不访问对象的成员,效率会更高。

例如,当创建要插入数据库的新对象时,它可能必须引用已存储在数据库中的另一个对象。

为了让 JPA 正确存储新对象,只需要引用对象的主键。
通过使用 getReference(),您可以获得一个包含主键的代理,并节省加载完整对象的成本。

getReference() does not retrieve the full object but only a proxy and therefore can be more efficient if you do not access the members of the object.

For instance when creating a new object to insert into your database, it might have to refer to another object which already has been stored in the database.

For JPA to store the new object correctly only the primary key of the referred object is needed.
By using getReference() you get a proxy which contains the primary key and you save the cost of loading the complete object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文