How to use table-per-subclass inheritance strategy instead of table-per-hierarchy

15 / Jun / 2008 by Deepak 3 comments

In this blog I want to share how to use table per sub-class instead of table per hierarchy, which is the default mechanism provided by Grails. I am not sure why Grails chose to use “table-per-hierarchy’. I always find it difficult to understand the table structure produced by “table-per-hierarchy“. Moreover, it makes things difficult for other non-grails application which need to read data from the data database. Another disadvantage is that columns cannot have a "Not Null" constraint applied to them at the db level.

If you are like me and prefer to use a table-per-subclass inheritance strategy, it can be easily achieved using fantastic Grails GORM DSL.

Simply include the following code in all your Grails domain classes.

   static mapping = {
   tablePerHierarchy false
}

This can be simply specified at the root class level and does-not need to be specified for all sub-classes in the hierarchy.

Hope this helps.

FOUND THIS USEFUL? SHARE IT

Tag -

GORM Grails

comments (3)

Leave a Reply

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