简体   繁体   English

分离实体和托管实体

[英]Detached Entity and Managed Entity

What a "detached entity" means? “独立实体”是什么意思? How is it possible to convert a managed entity to a detached entity during a transaction?如何在事务期间将受管实体转换为分离实体?

A detached entity is an entity whose state must not be reflected by the JPA provider.分离的实体是其状态不得由 JPA 提供程序反映的实体。

In other words, if you change its state (ie through setters methods) these changes will not be saved to the underlying database, as the JPA provider doesn't have to "observe" such entities.换句话说,如果您更改其状态(即通过 setter 方法),这些更改将不会保存到底层数据库中,因为 JPA 提供程序不必“观察”这些实体。

If entity E1 is a managed entity you can make it detached invoking (very reasonable named) method EntityManager#detach(E1) .如果实体 E1 是托管实体,您可以使其分离调用(非常合理的命名)方法EntityManager#detach(E1) You can also use EntityManager#clear() which will clear whole PersistenceContext and effectively making all managed entities detached.您还可以使用EntityManager#clear()来清除整个 PersistenceContext 并有效地分离所有托管实体。

actually, what is meant by a detached entity?实际上,分离的实体是什么意思?

Detached entity objects are objects in a special state in which they are not managed by any EntityManager but still represent objects in the database.分离的实体对象是处于特殊状态的对象,在这种状态下,它们不受任何EntityManager管理,但仍代表数据库中的对象。 Read more source阅读更多来源

and How it is possible convert a managed entity to a detached entity during a transaction?以及如何在事务期间将托管实体转换为分离实体?

Read here在这里阅读

Here you can read about JPA entity lifecycle.在这里您可以阅读有关 JPA 实体生命周期的信息。

Entity can be detached after serializing or closing of Persistence Context, for example.例如,可以在序列化或关闭持久性上下文后分离实体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM