////
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.
////
[[ormdsl-customCascadeBehaviour]]
== Custom Cascade Behaviour

Hibernate cascades control which persistence operations (save, update, delete, etc.) are automatically propagated from a parent entity to its associated children.

=== Default Cascade Behaviour

By default GORM applies `save-update` cascading on associations — when you save or update an entity, changes to its associated objects are also persisted. Deletions are **not** cascaded by default.

=== Configuring Cascade

Use the `cascade` option in the `mapping` block to override the default:

[source,groovy]
----
class Author {
    String name
    static hasMany = [books: Book]
    static mapping = {
        books cascade: 'all'        // <1>
    }
}
----
<1> `all` cascades all operations including delete to `books`.

[format="csv", options="header"]
|===
value,description
`all`,Propagates all operations (save/update/delete/merge/refresh)
`save-update`,Propagates save and update — default
`delete`,Propagates delete only
`all-delete-orphan`,Like `all` but also deletes child rows not present in the collection
`none`,No cascade
|===

=== Cascade Delete Example

[source,groovy]
----
class Author {
    String name
    static hasMany = [books: Book]
    static mapping = {
        books cascade: 'all-delete-orphan'  // <1>
    }
}
----
<1> When you remove a `Book` from `author.books` and call `author.save()`, the removed `Book` row is also deleted from the database.

TIP: Use `all-delete-orphan` when the child entity has no meaning outside the parent (composition). Use `save-update` (default) when the child may belong to multiple parents (aggregation).
