Jump to: navigation, search

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

Line 6: Line 6:
 
|api=y
 
|api=y
 
|apis=
 
|apis=
*[http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/annotations/CascadeOnDelete.html CascadeOnDelete]
+
*[http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/annotations/CascadeOnDelete.html @CascadeOnDelete]
 
|examples=y
 
|examples=y
 
|example=
 
|example=
Line 41: Line 41:
 
| ElementCollection mapping
 
| ElementCollection mapping
 
| The deletion of the collection table is cascaded on the database.
 
| The deletion of the collection table is cascaded on the database.
|}
+
|}}
  
  

Revision as of 09:12, 21 June 2011

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
Attribute Description Default Required?


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