discriminator

Purpose

Customizes the discriminator value used in table-per-subclass inheritance mapping.

Examples

class Content {
    …
}
class PodCast extends Content {
    …
    static mapping = {
        discriminator "audio"
    }
}

Description

Usage: discriminator(string/map)

Arguments:

By default when mapping inheritance Grails uses a single-table model so that all subclasses share the same table. A discriminator column is then used to help Grails tell what type a particular row is. By default the class name is stored as the value of this column. You can use the discriminator method to customize the value stored:

class Content {
    …
}
class PodCast extends Content {
    …
    static mapping = {
        discriminator "audio"
    }
}

You can also customize the discriminator column name:

class Content {
    …
    static mapping = {
        discriminator column:"content_type"
    }
}
class PodCast extends Content {
    …
    static mapping = {
        discriminator value: "audio"
    }
}

Or you can use a formula:

class Content {
    …
    static mapping = {
        discriminator value: "1", formula="case when CLASS_TYPE in ('a', 'b', 'c') then 0 else 1 end", type="integer"
    }
}
class PodCast extends Content{
    …
    static mapping = {
        discriminator value: "0"
    }
}