In this section we look at some more EntityManager
methods.
The remove()
method flags an entity for removal from the database. Note that the entity must be a managed entity. The removal need not take place immediately. Removal occurs whenever the EntityManager
decides to synchronize, or flush, the persistence context with the database. Typically this occurs when a transaction is committed. The code below shows a BankServiceBean
method, removeCustomer()
, which would be invoked in order to remove a Customer entity.
public void removeCustomer(Customer cust) { Customer mergedCust = em.merge(cust); em.remove(mergedCust); }
Note that the method first merges an entity to ensure it is managed before removing it.