9.4.2 persist operation for the entities
To execute the persist operation for the entities, invoke the persist method of EntityManager. If the persist method of EntityManager is invoked and the persist processing is cascaded, the entity is managed in the persistence and persistence context of the database.
The following table describes the state of the entity after the persist operation, for each entity state.
State of the entity |
State of the entity after the persist operation |
---|---|
new# |
The state changes to managed. The entity changed to managed is added into the database during or before transaction commit or is added into the database as a result of the execution of the flush operation. |
managed |
The persist operation is ignored. However, if PERSIST or ALL is specified in the cascade attribute for the relationship from one entity to another entity, the persist operation is propagated to the entities referenced by this entity. |
detached# |
If the line corresponding to the entity does not exist in the database, the state changes to managed. If the corresponding line exists, EntityExistsException occurs. |
removed |
The state changes to managed. |
#: For Cosminexus JPA Provider, when the state of an entity is new or detached, the results of the transition of entity states differ according to whether the data corresponding to the entity exists on the database. Note the following for Cosminexus JPA Provider:
-
If an entity that is not perpetuated in the database is specified in the argument, the state changes to managed.
-
If a line duplicating with the entity passed in an argument exists on the database and if that entity is managed with the persistence context, EntityExistsException occurs during the persist processing.
-
If a line duplicating with the entity passed in an argument exists on the database, but if the entity is not managed with the persistence context, EntityExistsException or another PersistenceException occurs during flush and commit.
- Important note
-
The entity is registered in the persistence context when the entity is perpetuated in the database and when the entity information is read from the database. Note that after the entity state is changed to managed, if the persistence processing is rolled back, the entity is not managed with the persistence context.