Deleting JPA Entities
You can delete existing entities from the database either explicitly by calling the removejakarta.persistence.EntityManager.remove(Object)Mark a managed entity instance as removed, resulting in its deletion from the database when the persistence context is synchronized with the database. method or implicitly as a result of a cascade operation.
Explicit remove
To delete an entity from the database, you must first retrieve it. Then, within an active transaction, call the removejakarta.persistence.EntityManager.remove(Object)Mark a managed entity instance as removed, resulting in its deletion from the database when the persistence context is synchronized with the database. method:
Employee employee = em.findjakarta.persistence.EntityManager.find(Class,Object)Find by primary key.(Employee.class, 1); em.getTransactionjakarta.persistence.EntityManager.getTransaction()Return the resource-level EntityTransaction object.().beginjakarta.persistence.EntityTransaction.begin()Start a resource transaction.(); em.removejakarta.persistence.EntityManager.remove(Object)Mark a managed entity instance as removed, resulting in its deletion from the database when the persistence context is synchronized with the database.(employee); em.getTransactionjakarta.persistence.EntityManager.getTransaction()Return the resource-level EntityTransaction object.().commitjakarta.persistence.EntityTransaction.commit()Commit the current resource transaction, writing any unflushed changes to the database.();
The entity is deleted from the database when the transaction is committed. Any embedded objects that the entity contains are also deleted. If the transaction is rolled back, the object is not deleted.
The removejakarta.persistence.EntityManager.remove(Object)Mark a managed entity instance as removed, resulting in its deletion from the database when the persistence context is synchronized with the database. method throws an IllegalArgumentException if its argument is not an entity instance or if it is a detached entity. It throws a TransactionRequiredExceptionjakarta.persistence.TransactionRequiredExceptionThrown by the persistence provider when a transaction is required but is not active. if called without an active transaction, because database modification operations require a transaction.
Cascading remove
Marking a reference field with CascadeTypejakarta.persistence.CascadeTypeDefines the set of cascadable operations that are propagated to the associated entity..REMOVEjakarta.persistence.CascadeType.REMOVECascade the remove operation (or CascadeTypejakarta.persistence.CascadeTypeDefines the set of cascadable operations that are propagated to the associated entity..ALLjakarta.persistence.CascadeType.ALLCascade all operations, which includes REMOVEjakarta.persistence.CascadeType.REMOVECascade the remove operation) indicates that removejakarta.persistence.EntityManager.remove(Object)Mark a managed entity instance as removed, resulting in its deletion from the database when the persistence context is synchronized with the database. operations automatically cascade to the entity or entities that the field references.
@Entityjakarta.persistence.EntityDeclares that the annotated class is an entity. class Employee { : @OneToOnejakarta.persistence.OneToOneSpecifies a single-valued association to another entity class that has one-to-one multiplicity.(cascadejakarta.persistence.OneToOne.cascade(Optional) The operations that must be cascaded to the target of the association.=CascadeTypejakarta.persistence.CascadeTypeDefines the set of cascadable operations that are propagated to the associated entity..REMOVEjakarta.persistence.CascadeType.REMOVECascade the remove operation) private Address address; : }
In the preceding example, the Employee entity has an address field that references an Address entity. Because of the CascadeType.REMOVE setting, when an Employee instance is removed, the operation automatically cascades to the referenced Address instance, which is also removed. Cascading can continue recursively to other referenced entities, such as any that the Address object references.
Orphan removal
JPA supports a more aggressive cascading removal mode that you can specify by using the orphanRemoval element of the @OneToOnejakarta.persistence.OneToOneSpecifies a single-valued association to another entity class that has one-to-one multiplicity. and @OneToManyjakarta.persistence.OneToManySpecifies a many-valued association with one-to-many multiplicity. annotations:
@Entityjakarta.persistence.EntityDeclares that the annotated class is an entity. class Employee { : @OneToOnejakarta.persistence.OneToOneSpecifies a single-valued association to another entity class that has one-to-one multiplicity.(orphanRemovaljakarta.persistence.OneToOne.orphanRemoval(Optional) Whether to apply the remove operation to entities that have been removed from the relationship and to cascade the remove operation to those entities.=true) private Address address; : }
When an Employee entity is removed, the remove operation cascades to the referenced Address entity. In this respect, orphanRemoval=true is identical to cascade=CascadeType.REMOVE. If you specify orphanRemoval=true, specifying CascadeType.REMOVE is redundant.
The difference between the two settings is how they handle a disconnected relationship, for example, when you set the address field to null or to another Address object.
- If
orphanRemoval=trueis specified, the disconnectedAddressinstance is automatically removed. This setting is useful for cleaning up dependent objects (such asAddress) that should not exist without a reference from an owner object (such asEmployee). - If only
cascade=CascadeType.REMOVEis specified, no automatic action is taken because disconnecting a relationship is not a remove operation.
To avoid dangling references, enable this feature only for fields that hold private, non-shared dependent objects.
You can also set orphan removal for collection and map fields. For example:
@Entityjakarta.persistence.EntityDeclares that the annotated class is an entity. class Employee { : @OneToManyjakarta.persistence.OneToManySpecifies a many-valued association with one-to-many multiplicity.(orphanRemovaljakarta.persistence.OneToMany.orphanRemoval(Optional) Whether to apply the remove operation to entities that have been removed from the relationship and to cascade the remove operation to those entities.=true) private List<Address> addresses; : }
In this case, removing an Address object from the collection automatically removes that object from the database.
DELETE Queries
DELETE queries provide an alternative way to remove entities from the database. They are especially useful for deleting many entities in a single operation. For more information, see DELETE Queries in JPA/JPQL.