Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 851
Shemil Avatar asked Dec 09 '11 05:12

Shemil


2 Answers

A detached entity is an entity whose state must not be reflected by the JPA provider.

In other words, if you change its state (i.e. 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.

like image 104
Piotr Nowicki Avatar answered Sep 28 '22 15:09

Piotr Nowicki


Here you can read about JPA entity lifecycle.

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

like image 27
Artem Avatar answered Sep 28 '22 16:09

Artem