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


== discriminator



=== Purpose


Customizes the discriminator column used in table-per-hierarchy inheritance mapping. Has no effect when using table-per-subclass inheritance mapping.


=== Examples


[source,groovy]
----
class Content {
    ...
}
----

[source,groovy]
----
class PodCast extends Content {
    ...
    static mapping = {
        discriminator "audio"
    }
}
----


=== Description


Usage: `discriminator(string/map)`

Arguments:

* `column` (optional) - The column name to store the discriminator
* `value` - The value to use for the discriminator
* `formula` (optional) - an SQL expression that is executed to evaluate the type of class. Use this or `column` but not both
* `type` (optional defaults to string) - the Hibernate type, used for the where clause condition to know if it needs to wrap it with '

By default when mapping inheritance Grails uses a single-table model where all classes share the same table. A discriminator column is used to determine the type for each row, by default the full class name. You can use the `discriminator` method to customize what's stored:

[source,groovy]
----
class Content {
    ...
}
----

[source,groovy]
----
class PodCast extends Content {
    ...
    static mapping = {
        discriminator "audio"
    }
}
----

You can also customize the discriminator column name:

[source,groovy]
----
class Content {
    ...
    static mapping = {
        discriminator column: "content_type"
    }
}
----

[source,groovy]
----
class PodCast extends Content {
    ...
    static mapping = {
        discriminator value: "audio"
    }
}
----

Or you can use a formula:

[source,groovy]
----
class Content {
    ...
    static mapping = {
        discriminator value: "1", type: "integer",
            formula: "case when CLASS_TYPE in ('a', 'b', 'c') then 0 else 1 end",
    }
}
----

[source,groovy]
----
class PodCast extends Content {
    ...
    static mapping = {
        discriminator value: "0"
    }
}
----
