Discriminator in Grails inheritance

03 / Sep / 2012 by Uday Pratap Singh 1 comments

Whenever I have the use case of inheritance in my Grails application I prefer to have the single table for all the classes, to avoid joins and multiple save statement. But one thing I dont like about this is its class discriminator which have the full class name in it. I dont find it much readable when looking into the sql.

 

Grails provide cool mapping to customise the discriminator in Grails inheritance. To explain this I am taking the example  of  my last blog . So to change the value of discriminator I just need to add the discriminator mapping in every sub class and give them some unique identification value

[java]
static mapping = {
discriminator "TextBlog"
}
[/java]

Now the class discriminator will store “TextBlog” rather than full class name e.g; com.intelligrape.example.TextBlog.

After doing this I change the discriminator column name because its not actually storing the class, so I add following mapping to my base class to change column name.

[java]
static mapping = {
discriminator column: "type"
}
[/java]

After doing this the column name will change from “class” to “type”.

 

Hope it helps

Uday Pratap Singh
uday@intelligrape.com

FOUND THIS USEFUL? SHARE IT

comments (1 “Discriminator in Grails inheritance”)

  1. mohit shindfe

    Hi,
    I am looking to set value of descriminator at dynamic; as i want to decide it depending upon some selection.
    Is i possible or not itf yes please guide me.
    As we can change column name can we change value

    Thanks,
    Mohit Shinde

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *