////
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
////


== cascade



=== Purpose


Configures the cascading behavior of an association.


=== Examples


[source,groovy]
----
class Author {

    static hasMany = [books: Book]

    static mapping = {
        books cascade: 'all-delete-orphan'
    }
}
----


=== Description


Usage: `association_name(cascade:string)`

Arguments:

* `cascade` - The cascading behavior to define. Can be one or more (comma-separated) of `all`, `merge`, `save-update`, `delete`, `lock`, `refresh`, `evict`, `replicate` or `all-delete-orphan` (one-to-many associations only).

By default GORM configures a cascading policy of "all" in the case where one entity "belongs to" another. For example:

[source,groovy]
----
class Book {
    static belongsTo = [author: Author]
}
----

[source,groovy]
----
class Author {
    static hasMany = [books: Book]
}
----

Here all persistence operations will cascade from the `Author` domain to the `Book` domain. So when an `Author` is deleted so will all the associated books.

If the association doesn't define an owner (a "belongs to" relationship):

[source,groovy]
----
class Book {
}
----

[source,groovy]
----
class Author {
    static hasMany = [books: Book]
}
----

then GORM uses a cascading policy of "save-update" by default. So if an `Author` is deleted the `Book` associated domains won't be deleted. Use the `cascade` argument on an association to customize this behavior:

[source,groovy]
----
class Author {

    static hasMany = [books: Book]

    static mapping = {
        books cascade: 'all-delete-orphan'
    }
}
----

Using this configuration a `Book` will also be deleted if it is removed (orphaned) from an `Author`'s `books` association.

See the section on https://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/objectstate.html#objectstate-transitive[transitive persistence] in the Hibernate user guide.
