简体   繁体   中英

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.

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.

If entity E1 is a managed entity you can make it detached invoking (very reasonable named) method EntityManager#detach(E1) . You can also use EntityManager#clear() which will clear whole PersistenceContext and effectively making all managed entities detached.

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. 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.

Entity can be detached after serializing or closing of Persistence Context, for example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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