Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Difference between revisions of "EclipseLink/UserGuide/JPA/Advanced JPA Development/Schema Generation/CascadeOnDelete"
< EclipseLink | UserGuide | JPA | Advanced JPA Development | Schema Generation
Line 15: | Line 15: | ||
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>cascade-on-delete</tt> XML element 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 or the <tt><cascade-on-delete></tt> XML element to specify that a delete operation performed on a database object is cascaded on secondary or related tables. |
{| cellpadding="2" border="1" | {| cellpadding="2" border="1" | ||
Line 30: | Line 30: | ||
|- | |- | ||
| OneToMany mapping | | OneToMany mapping | ||
− | | | + | | For a OneToMany using a mappedBy of 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). | ||
|- | |- | ||
− | | ElementCollection | + | | ElementCollection mapping |
| The deletion of the collection table is cascaded on the database. | | The deletion of the collection table is cascaded on the database. | ||
|} | |} | ||
Line 53: | Line 54: | ||
* ManyToMany : SQL will not be executed to delete from the join table. | * ManyToMany : SQL will not be executed to delete from the join table. | ||
* ElementCollection : SQL will not be executed to delete from the collection 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. | ||
= Configuration File = | = Configuration File = | ||
In the <tt>orm.xml</tt> descriptor file, specify cascade on delete as follows: | In the <tt>orm.xml</tt> descriptor file, specify cascade on delete as follows: | ||
− | < | + | <source lang="xml"> |
− | + | <cascade-on-delete>true</cascade-on-delete> | |
− | + | </entity> | |
− | + | </source> | |
− | + | ||
− | + | ||
− | </ | + | |
<br> {{EclipseLink_JPA | <br> {{EclipseLink_JPA | ||
|previous =[[EclipseLink/UserGuide/JPA/Advanced_JPA_Development/Schema_Generation|Schema Generation]] | |previous =[[EclipseLink/UserGuide/JPA/Advanced_JPA_Development/Schema_Generation|Schema Generation]] |
Revision as of 14:53, 1 February 2011
EclipseLink JPA
EclipseLink | |
Website | |
Download | |
Community | |
Mailing List • Forums • IRC • mattermost | |
Issues | |
Open • Help Wanted • Bug Day | |
Contribute | |
Browse Source |
Key API CascadeOnDelete
@CascadeOnDelete
>>>>>> THIS PAGE IS IN PROGRESS. PLEASE SEE DISCUSSION PAGE. <<<<<<
ON DELETE CASCADE is a database foreign key constraint option that automatically removes the dependent rows.
Use the @CascadeOnDelete annotation or the <cascade-on-delete> XML element to specify that a delete operation performed on a database object is cascaded on secondary or related tables.
Performing a CascadeOnDelete on this object... | Does this... | |
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-foriegn key OneToOne mappings (because of constraint direction). | |
OneToMany mapping | For a OneToMany using a mappedBy of 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 | 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. |
@Target(value={METHOD,FIELD,TYPE}) @Retention(value=RUNTIME) public @interface CascadeOnDelete
Usage of @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.
Configuration File
In the orm.xml descriptor file, specify cascade on delete as follows:
<cascade-on-delete>true</cascade-on-delete> </entity>