相当于Grails中的InheritanceType.TABLE_PER_CLASS?

时间:2014-03-17 04:59:09

标签: java hibernate grails gorm grails-domain-class

我想为3个域类创建3个单独的表:A,B扩展A,C扩展B 但我希望他们的桌子不相互连接。

在hibernate中,我会在grails中使用InheritanceType.TABLE_PER_CLASS,它会等同于什么?

2 个答案:

答案 0 :(得分:0)

尝试使用 tablePerHierarchy false

class Payment {
    Integer amount
    static mapping = {
        tablePerHierarchy false
    }
}

class CreditCardPayment extends Payment {
    String cardNumber
}

查看更多:http://grails.org/doc/latest/guide/single.html#5.5.2.3%20Inheritance%20Strategies

答案 1 :(得分:0)

我试图实现的东西,是的,有可能用grails,这是一个弯曲勺子一点实现它:

在grails 3下执行此操作

您有一个您希望扩展的基类,并且具有相同字段的子表,即TABLE_PER_CLASS:

我发现了另一个有帮助的帖子,但有一条评论,但现在在这篇文章中正确扩展和解释:

这可能是我们的基础域类,但它不是在grails-app/domains/中创建的,而是在src/main/groovy/test下创建的

package test

abstract class Tester {
    String name
    String something

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

接下来是grails-app/domains/test中的实际域类:

package  test
class Tester1 extends Tester {
}

package  test
class Tester2 extends Tester {
}

看着mysql:

mysql> show create table tester2;
+---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table   | Create Table                                                                                                                                                                                                                                                            |
+---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tester2 | CREATE TABLE `tester2` (
  `id` bigint(20) NOT NULL,
  `something` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci |
+---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> show create table tester1;
+---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table   | Create Table                                                                                                                                                                                                                                                            |
+---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tester1 | CREATE TABLE `tester1` (
  `id` bigint(20) NOT NULL,
  `something` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci |
+---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)