Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
EclipseLink/DesignDocs/324341
Design Specification: On Delete Cascade
Document History
Date | Author | Version Description & Notes |
---|---|---|
2010-09-02 | James | 0.1 Draft |
Project overview
Allow delete cascading to be defined and handled by the database.
Concepts
ON DELETE CASCADE is a database foreign key constraint option that automatically removes the dependent rows.
Requirements
Support specifying that a descriptor's secondary tables, or related table cascade the delete on the database. Support generating DDL to cascade deletion in auto DDL generation. Support omitting delete SQL for multiple table and joined inheritance descriptors. Support omitting delete SQL for relationships, 1-1, 1-m, m-m, dc, ac.
Design Constraints
Must be database independent.
Functionality
An @CascadeOnDelete annotation will be added. It can be set on an Entity, or relationship. On an Entity it defines that secondary or joined inheritance tables should cascade the delete on the database. On a OneToOne the related object is deleted cascaded on the database. This is only allowed for mappedBy/target-foriegn key 1-1s (because of constraint direction). On a OneToMany the related objects are deleted cascaded on the database. On a ManyToMany the join table is deleted cascaded on the database (target objects cannot be cascade even if private because of constraint direction). On a DirectCollection, direct table is deleted cascaded on the database. On a AggregateCollection, aggregate table is deleted cascaded on the database.
Auto DDL generation will set the onDeleteCascade flag on the ForeignKeyDefinition based on the mapping.
DeleteObject and DeleteAll queries will check the descriptor and only delete from the first table if cascaded.
DirectCollection, AggregateCollection and ManyToMany will avoid executing the delete SQL if cascaded.
OneToOne and OneToMany mappings will record their dependencies as having been delete on the database in the unit of work. Deletion will then check this to avoid executing the SQL. Note that the DeleteObject queries will still execute as normal, just no SQL will occur.
Testing
Add CascadeOnDelete to some DDL generation model classes. Add CascadeOnDelete to fieldaccess model classes and add deletion tests.
API
- ClassDescriptor.setIsCascadeOnDeleteSetOnDatabaseOnSecondaryTables()
- ForeignReferenceMapping.setIsCascadeOnDeleteSetOnDatabase()
- @CascadeOnDelete
Config files
- orm.xml
<entity cascadeOnDelete="true"> <one-to-one cascadeOnDelete="true"> <one-to-many cascadeOnDelete="true"> <many-to-many cascadeOnDelete="true">
Documentation
Should be documented under extended annotations, and schema generation. Also needs to be linked under cascade remove and remove orphans.
Open Issues
Issue # | Owner | Description / Notes |
---|---|---|
1 | What databases support ON DELETE CASCADE, do any use a different syntax? | |
2 | On lock errors will occur on delete cascades for relatinoships, should not be used with optimistic locking on 1-1, 1-m? |
Decisions
Issue # | Description / Notes | Decision |
---|
Future Considerations
- Other constraint options such as ON DELETE NULL.