ON DELETE CASCADE is a database foreign key constraint option that automatically removes the dependent rows.

ON DELETE CASCADE is a database foreign key constraint option that automatically removes the dependent rows.

−

Use the <tt>@CascadeOnDelete</tt> annotation or the <tt>cascadeOnDelete</tt> property to specify that a delete operation performed on a database object is cascaded on secondary or related tables.

+

Use the [http://www.eclipse.org/eclipselink/api/latest/eclipse/persistence/annotations/CascadeOnDelete.html <tt>@CascadeOnDelete</tt>] annotation to specify that a delete operation performed on a database object is cascaded on secondary or related tables.

−

{| cellpadding="2" border="1"

+

=====''CascadeOnDelete Behavior''=====

−

|+ ''CascadeOnDelete''

+

{| cellpadding="2" border="1" cellspacing=0

|-

|-

−

| '''Performing a CascadeOnDelete on this object...'''

+

| '''Entity/Mapping'''

−

| '''Does this...'''

+

| '''Behavior'''

|-

|-

| Entity

| Entity

Line 27:

Line 35:

|-

|-

| OneToOne mapping

| OneToOne mapping

−

| The deletion of the related object is cascaded on the database. This is only allowed for mappedBy/target-foriegn key OneToOne mappings (because of constraint direction).

+

| The deletion of the related object is cascaded on the database.

+

This is only allowed for mappedBy/target-foriegn key OneToOne mappings (because of constraint direction).

|-

|-

| OneToMany mapping

| OneToMany mapping

−

| The deletion of the related objects is cascaded on the database.

+

| For a OneToMany using a mappedBy or JoinColumn, the deletion of the related objects is cascaded on the database.

+

For a OneToMany using a JoinTable, the deletion of the join table is cascaded on the database (target objects cannot be cascaded even if private because of constraint direction).

|-

|-

| ManyToMany mapping

| 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).

+

| The deletion of the join table is cascaded on the database (target objects cannot be cascaded even if private because of constraint direction).

|-

|-

−

| DirectCollection

+

| ElementCollection mapping

−

| The deletion of the direct table is cascaded on the database.

+

| The deletion of the collection table is cascaded on the database.

−

|-

+

−

| AggregateCollection

+

−

| The deletion of the aggregate table is cascaded on the database.

+

|}

|}

−

<br> <source lang="java">

−

@Target(value={METHOD,FIELD,TYPE})

−

@Retention(value=RUNTIME)

−

public @interface CascadeOnDelete

−

</source>

−

<br>

+

<tt>@CascadeOnDelete</tt> 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.

+

+

== @CascadeOnDelete Example ==

+

This example cascade the deletion of the Employee secondary table, and all of its owned relationships.