How Grails gorm will insert table per concrete cla

2019-02-19 23:07发布

问题:

Hi all following is the situation I have an abstract class AbstractProfile and one concrete class GoogleProfile

abstract class AbstractProfile  {
    .....
}

class GoogleProfile extends AbstractProfile { 

    ......
}

I am using grails but gorm is not inserting table for google profile current gorm is inserting table for only AbstractProfile class please help Thanks in advance

回答1:

I did some digging and found out that starting on grails 2.3 you have the following mapping option:

tablePerConcreteClass true

It seems the documentation (even for version 2.4) hasn't been updated regarding this yet.



回答2:

You can use this :

abstract class BaseDomain {
            static mapping = {
                tablePerConcreteClass true
                id generator: 'increment'
                version false
            }
    }


回答3:

Grails 2.0 persists abstract classes. In order to enable an individual table to the extending class, you need to specify:

static mapping = {
        tablePerHierarchy       false
}

to the abstract class. Otherwise the whole hierarchy will "live" in the same table.



回答4:

Following up on Михаил's great answer, two separate things worked for me.

  1. Putting the base domain class in src

This worked best for me because both the constraints from the base class and the subclass were applied. Having not null columns in the subclasses was important for my use case. However, it appears that only the mapping block from the subclass is used.

  1. Using tablePerConcreteClass true

An advantage here is it allows an index to be declared in the BaseDomain class that then appears in each of the subclass tables, ie both the mapping blocks from the base class and subclass are used. Only the constraints in the base class appear to be used.

abstract class BaseDomain {
    static mapping = {
        tablePerHierarchy false  // avoid creating the base_domain table
        tablePerConcreteClass true
        id generator: 'increment' // https://jira.grails.org/browse/GRAILS-10849
        someColumnInBaseDomain index: true  // index this column in each subclass table
    }
}


标签: grails gorm