Difference between revisions of "EclipseLink/UserGuide/JPA/Advanced JPA Development/Schema Generation/CascadeOnDelete"

From Eclipsepedia

Jump to: navigation, search
m
Line 52: Line 52:
 
* Automatic DDL generation sets the <tt>onDeleteCascade</tt> flag on the <tt>ForeignKeyDefinition</tt> based on the mapping.
 
* Automatic DDL generation sets the <tt>onDeleteCascade</tt> flag on the <tt>ForeignKeyDefinition</tt> based on the mapping.
  
<tt>DeleteObject</tt> and <tt>DeleteAll</tt> queries check the descriptor and only delete from the first table if cascaded.
+
* <tt>DeleteObject</tt> and <tt>DeleteAll</tt> queries check the descriptor and only delete from the first table if cascaded.
  
<tt>DirectCollection</tt>, <tt>AggregateCollection</tt> and <tt>ManyToMany</tt> do not execute the delete SQL, if cascaded.
+
* <tt>DirectCollection</tt>, <tt>AggregateCollection</tt> and <tt>ManyToMany</tt> do not execute the delete SQL, if cascaded.
  
<tt>OneToOne</tt> and <tt>OneToMany</tt> mappings record their dependencies as having been a delete on the database in the Unit of Work. Deletion then checks this to prevent executing the SQL. Note that <tt>DeleteObject</tt> queries still execute as normal, except that no SQL will be executed.  
+
* <tt>OneToOne</tt> and <tt>OneToMany</tt> mappings record their dependencies as having been a delete on the database in the Unit of Work. Deletion then checks this to prevent executing the SQL. Note that <tt>DeleteObject</tt> queries still execute as normal, except that no SQL will be executed.  
  
 
= Configuration File  =
 
= Configuration File  =

Revision as of 11:30, 1 February 2011

EclipseLink JPA

link="http://wiki.eclipse.org/EclipseLink"
EclipseLink
Website
Download
Community
Mailing ListForumsIRC
Bugzilla
Open
Help Wanted
Bug Day
Contribute
Browse Source

Elug api package icon.png 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 cascadeOnDelete property to specify that a delete operation performed on a database object is cascaded on secondary or related tables.

CascadeOnDelete
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 The deletion of the related objects is cascaded on the database.
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).
DirectCollection The deletion of the direct table is cascaded on the database.
AggregateCollection The deletion of the aggregate table is cascaded on the database.

@Target(value={METHOD,FIELD,TYPE})
@Retention(value=RUNTIME)
public @interface CascadeOnDelete

Cascading deletions behave as follows:

  • Automatic DDL generation sets the onDeleteCascade flag on the ForeignKeyDefinition based on the mapping.
  • DeleteObject and DeleteAll queries check the descriptor and only delete from the first table if cascaded.
  • DirectCollection, AggregateCollection and ManyToMany do not execute the delete SQL, if cascaded.
  • OneToOne and OneToMany mappings record their dependencies as having been a delete on the database in the Unit of Work. Deletion then checks this to prevent executing the SQL. Note that DeleteObject queries still execute as normal, except that no SQL will be executed.

Configuration File

In the orm.xml descriptor file, specify cascade on delete as follows:

<source lang="xml">
<entity cascadeOnDelete="true">
<one-to-one cascadeOnDelete="true">
<one-to-many cascadeOnDelete="true">
<many-to-many cascadeOnDelete="true">
</source>

Eclipselink-logo.gif
Version: 2.2.0 DRAFT
Other versions...