{"id":7050,"date":"2012-09-03T18:08:49","date_gmt":"2012-09-03T12:38:49","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=7050"},"modified":"2016-12-19T15:06:22","modified_gmt":"2016-12-19T09:36:22","slug":"discriminator-in-grails-inheritance","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/discriminator-in-grails-inheritance\/","title":{"rendered":"Discriminator in Grails inheritance"},"content":{"rendered":"<p>Whenever I have the use case of inheritance\u00a0in 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.<\/p>\n<p>&nbsp;<\/p>\n<p>Grails provide cool mapping to customise the <a href=\"http:\/\/grails.org\/doc\/latest\/ref\/Database%20Mapping\/discriminator.html\" target=\"_blank\">discriminator<\/a> in Grails inheritance. To explain this I am taking the example \u00a0of \u00a0my last <a href=\"http:\/\/www.tothenew.com\/blog\/inheritance-in-grails\/\" target=\"_blank\">blog <\/a>.\u00a0So 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<\/p>\n<p>[java]<br \/>\nstatic mapping = {<br \/>\n        discriminator &quot;TextBlog&quot;<br \/>\n    }<br \/>\n[\/java]<\/p>\n<p>Now the class discriminator will store &#8220;TextBlog&#8221; rather than full class name e.g; com.intelligrape.example.TextBlog.<\/p>\n<p>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.<\/p>\n<p>[java]<br \/>\nstatic mapping = {<br \/>\n        discriminator column: &quot;type&quot;<br \/>\n    }<br \/>\n[\/java]<\/p>\n<p>After doing this the column name will change from &#8220;class&#8221; to &#8220;type&#8221;.<\/p>\n<p>&nbsp;<\/p>\n<p>Hope it helps<\/p>\n<p><a href=\"http:\/\/www.tothenew.com\/blog\/author\/uday\/\">Uday Pratap Singh<\/a><br \/>\n<a href=\"mailto:uday@intelligrape.com\">uday@intelligrape.com<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Whenever I have the use case of inheritance\u00a0in 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 [&hellip;]<\/p>\n","protected":false},"author":11,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":3},"categories":[7],"tags":[932,4840,930,931],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/7050"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=7050"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/7050\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=7050"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=7050"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=7050"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}