////
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.
////
[[domainClasses-gormComposition]]
== GORM Composition (Embedded Objects)

GORM supports composition via `embedded`, which maps a non-domain class as a set of columns on the owning table rather than a separate table.

=== Declaring an Embedded Component

[source,groovy]
----
class Address {
    String street
    String city
    String postalCode
    String country
}

class Person {
    String name
    Address address             // <1>

    static embedded = ['address']   // <2>
}
----
<1> `Address` is a plain Groovy class (not a domain class).
<2> Declaring it in `embedded` maps its properties as columns on the `person` table.

The `person` table will contain columns: `name`, `address_street`, `address_city`, `address_postal_code`, `address_country`.

=== Overriding Column Names

Use the `mapping` block to rename the embedded columns:

[source,groovy]
----
class Person {
    static embedded = ['address']
    static mapping = {
        address {
            street  column: 'addr_street'
            city    column: 'addr_city'
        }
    }
}
----

=== Nullable Embedded Objects

If the embedded object can be absent, mark it as nullable in constraints:

[source,groovy]
----
class Person {
    Address address
    static embedded  = ['address']
    static constraints = {
        address nullable: true
    }
}
----

NOTE: Embedded components are always persisted as part of the owning entity. There is no separate table, no `id`, and no lifecycle management for the embedded object.
