How Grails gorm will insert table per concrete cla

2019-02-19 22:26发布

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

标签: grails gorm
4条回答
聊天终结者
2楼-- · 2019-02-19 22:51

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.

查看更多
beautiful°
3楼-- · 2019-02-19 22:55

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.

查看更多
来,给爷笑一个
4楼-- · 2019-02-19 23:02

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
    }
}
查看更多
Fickle 薄情
5楼-- · 2019-02-19 23:14

You can use this :

abstract class BaseDomain {
            static mapping = {
                tablePerConcreteClass true
                id generator: 'increment'
                version false
            }
    }
查看更多
登录 后发表回答