The 30 second answer
Removed is a state inside the persistence context: the entity still has its identifier and the context still knows it, it's just scheduled for deletion. Deleted means the DELETE has actually run in the database. Because the persistence context works as a write-behind cache, the DELETE runs at flush, which by default is before commit, and becomes permanent at commit. So there's a gap between calling remove and the row really disappearing.
The Hibernate docs define the removed state like this: the entity has an identifier and is associated with a persistence context, but it is scheduled for removal from the database. So calling remove has not deleted anything from the database yet.
The actual deletion is the job of flush. The persistence context is a transactional write-behind cache, and flush turns each change into an INSERT, UPDATE or DELETE. In the default AUTO flush mode, that flush happens before commit. It can also happen earlier, for example before a JPQL query that overlaps with the change. Once the transaction commits, the deletion is permanent.
There is also an API difference: EntityManager.remove only accepts a managed entity, while Hibernate's Session.delete also accepts a detached one.
Code: the DELETE runs at flush, not at remove.
@Transactional
public void delete(Long id) {
Order order = em.find(Order.class, id);
em.remove(order); // removed
em.flush(); // DELETE runs now (otherwise at commit)
} // permanent once the transaction commitsWithout the explicit flush(), the DELETE would run at the automatic flush just before commit. Until then, the entity is only removed, and the row is still in the database.
Sources (3)
- Hibernate ORM User Guide: Persistence Context (entity states)docs.jboss.org
- Hibernate ORM User Guide: Deleting (removing) entitiesdocs.jboss.org
- Hibernate ORM User Guide: Flushingdocs.jboss.org