Jump to: navigation, search

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

m
m
(37 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 +
----
 +
 +
 +
'''[[Image:Elug_draft_icon.png|Warning]] This page is now obsolete. '''
 +
 +
For current information, please see "@CascadeOnDelete" in the ''Java Persistence API (JPA) Extensions Reference for EclipseLink'': http://eclipse.org/eclipselink/documentation/latest/jpa/extensions/a_cascadeondelete.htm
 +
 +
 +
----
 +
 +
 +
 +
 +
 
{{EclipseLink_UserGuide
 
{{EclipseLink_UserGuide
 
|info=y
 
|info=y
Line 5: Line 19:
 
|eclipselinktype=JPA
 
|eclipselinktype=JPA
 
|api=y
 
|api=y
|apis=[http://www.eclipse.org/eclipselink/api/latest/eclipse/persistence/annotations/CascadeOnDelete.html CascadeOnDelete]
+
|apis=
}}
+
*[http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/annotations/CascadeOnDelete.html @CascadeOnDelete]
=@CascadeOnDelete=
+
|examples=y
 +
|example=
 +
*[[EclipseLink/Examples/JPA/DeleteCascade|DeleteCascade Example]]
 +
}}
  
--THIS PAGE IS IN PROGRESS --
+
= @CascadeOnDelete  =
  
 
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  
+
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.  
on secondary or related tables.  
+
  
{| border="1" cellpadding="2"
+
=====''CascadeOnDelete Behavior''=====
|+ ''CascadeOnDelete''
+
{| cellpadding="2" border="1" cellspacing=0
|-
+
| '''On this...''" ||'''CascadeOnDelete does this...'''
+
 
|-
 
|-
| Entity|| Defines that secondary or joined inheritance tables should cascade the delete on the database
+
| '''Entity/Mapping'''
 +
| '''Behavior'''
 
|-
 
|-
| OneToOne mapping|| The related object is deleted cascaded on the database. This is only allowed for mappedBy/target-foriegn key OneToOne mappings (because of constraint direction).
+
| Entity
 +
| Defines that secondary or joined inheritance tables should cascade the delete on the database
 
|-
 
|-
|OneToMany mapping || The related objects are deleted cascaded 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).
 
|-
 
|-
|ManyToMany mapping || The join table is deleted cascaded on the database (target objects cannot be cascade even if private because of constraint direction).  
+
| OneToMany mapping  
 +
| 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).
 
|-
 
|-
|DirectCollection || The direct table is deleted 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).
 
|-
 
|-
|AggregateCollection||The aggregate table is deleted cascaded on the database.
+
| ElementCollection mapping
 +
| The deletion of the collection table is cascaded on the database.
 
|}
 
|}
  
  
 +
<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.
 
<source lang="java">
 
<source lang="java">
@Target(value={METHOD,FIELD,TYPE})
+
@Entity
@Retention(value=RUNTIME)
+
@SecondaryTable(name="EMP_SALARY")
public @interface CascadeOnDelete
+
@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;
 +
    ...
 +
}
 
</source>
 
</source>
  
 +
== Specifying Cascade on Delete in eclipselink-orm.xml ==
  
= Config files =
+
In the <tt>eclipselink-orm.xml</tt> descriptor file, specify cascade on delete as shown in the following example:  
In the <tt>orm.xml</tt> descriptor file, specify cascade on delete as follows:
+
 
<source lang="xml">
 
<source lang="xml">
<entity cascadeOnDelete="true">
+
<cascade-on-delete>true</cascade-on-delete>
<one-to-one cascadeOnDelete="true">
+
<one-to-many cascadeOnDelete="true">
+
<many-to-many cascadeOnDelete="true">
+
 
</source>
 
</source>
  
{{EclipseLink_JPA
+
See also, [[EclipseLink/Examples/JPA/DeleteCascade|DeleteCascade Example]]
|previous =[[EclipseLink/UserGuide/JPA/Advanced_JPA_Development/Schema_Generation|Schema Generation]]
+
<br> {{EclipseLink_JPA
|up       =[[EclipseLink/UserGuide/JPA/Advanced_JPA_Development/Schema_Generation|Schema Generation]]
+
|previous=[[EclipseLink/UserGuide/JPA/Advanced_JPA_Development/Schema_Generation|Schema Generation]]
|next =[[EclipseLink/UserGuide/JPA/Advanced_JPA_Development/Schema_Generation/Index|@Index]]
+
|up     =[[EclipseLink/UserGuide/JPA/Advanced_JPA_Development/Schema_Generation|Schema Generation]]
 +
|next =[[EclipseLink/UserGuide/JPA/Advanced JPA Development/Schema Generation/Index|@Index]]
 
|version=2.2.0 DRAFT}}
 
|version=2.2.0 DRAFT}}

Revision as of 06:28, 17 April 2013



Warning This page is now obsolete.

For current information, please see "@CascadeOnDelete" in the Java Persistence API (JPA) Extensions Reference for EclipseLink: http://eclipse.org/eclipselink/documentation/latest/jpa/extensions/a_cascadeondelete.htm






EclipseLink JPA

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


@CascadeOnDelete

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

Use the @CascadeOnDelete annotation to specify that a delete operation performed on a database object is cascaded on secondary or related tables.

CascadeOnDelete Behavior
Entity/Mapping Behavior
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 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 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.

@CascadeOnDelete Example

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

@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;
    ...
}

Specifying Cascade on Delete in eclipselink-orm.xml

In the eclipselink-orm.xml descriptor file, specify cascade on delete as shown in the following example:

<cascade-on-delete>true</cascade-on-delete>

See also, DeleteCascade Example


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