Use the @CascadeOnDelete annotation to specify that a delete operation performed on a database object is cascaded on secondary or related tables.
ON DELETE CASCADE is a database foreign key constraint option that automatically removes the dependent rows.
Annotation Elements
There are no elements for this annotation.
Usage
You can place @CascadeOnDelete on any relationship in which the target is defined as foreign key to the source Entity.
Add the annotation on the source relationship: @OneToOne, @OneToMany, @ManyToMany, and @ElementCollection You can also add @CascadeOnDelete to an Entity with a @SecondaryTable or JOINED inheritance. Table 2-8 describes the affect of placing @CascadeDelete on these different elements
Table 2-8 Using @Cascade on Different Elements
| Element | Effect of @CascadeOnDelete |
|---|---|
|
Entity |
Defines that secondary or joined inheritance tables should cascade the delete on the database |
|
OneToOne mapping |
The deletion of the related object is cascaded on the database. This is only allowed for mappedBy/target-foreign key OneToOne mappings (because of constraint direction). |
|
OneToMany mapping |
For a OneToMany using a For a OneToMany using a |
|
ManyToMany mapping |
The deletion of the join table is cascaded on the database (target objects cannot be cascaded even if private because of constraint direction). |
|
ElementCollection mapping |
The deletion of the collection table is cascaded on the database. |
@CascadeOnDelete has the following behavior:
DDL generation: If DDL generation is used, the generated constraint will include the cascade deletion option.
Entity: Remove will not execute SQL for deletion from secondary or joined inheritance tables (as constraint will handle deletion).
OneToOne: If the mapping uses cascading or orphanRemoval, SQL will not be executed to delete target object.
OneToMany: If the mapping uses cascading or orphanRemoval, SQL will not be executed to delete target objects.
ManyToMany: SQL will not be executed to delete from the join table.
ElementCollection: SQL will not be executed to delete from the collection table.
Cache: Cascaded objects will still be removed from the cache and persistence context.
Version locking: Version will not be verified on deletion of cascaded object.
Events: Deletion events may not be executed on the cascaded objects if the objects are not loaded.
Cascading: The remove operation should still be configured to cascade in the mapping if using CascadeOnDelete.
Examples
Example 2-18 shows the cascading deletion of the Employee secondary table and all of its owned relationships.
Example 2-18 Using @CascadeOnDelete Annotation
@Entity
@SecondaryTable(name="EMP_SALARY")
@CascadeOnDelete
public class Employee{
@Id
private long id;
private String firstName;
private String lastName;
@Column(table="EMP_SALARY")
private String salary;
@OneToOne(mappedBy="owner", orphanRemoval=true, cascade={CascadeType.ALL})
@CascadeOnDelete
private Address address;
@OneToMany(mappedBy="owner", orphanRemoval=true, cascade={CascadeType.ALL})
@CascadeOnDelete
private List<Phone> phones;
@ManyToMany
@JoinTable(name="EMP_PROJ")
@CascadeOnDelete
private List<Project> projects;
...
}
In the eclipselink-orm.xml descriptor file, specify cascade on delete as shown in Example 2-19
See Also
For more information, see:
EclipseLink example: http://wiki.eclipse.org/EclipseLink/Examples/JPA/DeleteCascade
"@CascadeOnDelete"
http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Advanced_JPA_Development/Schema_Generation/CascadeOnDelete
"Enhancing Performance" in Solutions Guide for EclispeLink