相当于Grails中的InheritanceType.TABLE_PER_CLASS? [英] equivalent of InheritanceType.TABLE_PER_CLASS in grails?

查看:145
本文介绍了相当于Grails中的InheritanceType.TABLE_PER_CLASS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



在hibernate中,我会使用InheritanceType.TABLE_PER_CLASS,在grails中,什么是等效的?

我试图也实现的东西,是的,它可能与Grails,这是一个弯曲勺子一点实现它的情况:

在grails 3下执行此操作



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

我发现另一篇文章有​​帮助,但有一条评论,但现在在这篇文章中正确地扩展和解释它:



这将是我们的基础域类,而不是创建它位于 src / main / groovy / test

之下 grails-app / domains / b
$ b

 包测试

abst ract class Tester {
字符串名称
字符串

静态映射= {
tablePerConcreteClass true
id生成器:​​'increment'
version false


接下来将

  package test 
class Tester1 extends Tester> grails-app / domains / test

{
}

  package test 
class Tester2 extends Tester {
}

查看mysql:

  mysql>显示创建表tester2; 
+ --------- + ----------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- ------------------------------ +
|表|创建表|
+ --------- + ----------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- ------------------------------ +
| 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行(0.00秒)

mysql> show create table tester1;
+ --------- + ----------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- ------------------------------ +
|表|创建表|
+ --------- + ----------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- ------------------------------ +
| 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行(0.00秒)


I want to create 3 separate tables for 3 domain classes: A, B extends A, C extends B But I want their tables to be NOT connected to each other.

In hibernate, I would use InheritanceType.TABLE_PER_CLASS, in grails, what would be the equivalent?

解决方案

Something I was trying to also achieve and yes it is possible with grails, it is a case of bending the spoon a little to achieve it:

Doing this under grails 3

You have a base class that you wish to extend and have child tables that have identical fields i.e. TABLE_PER_CLASS:

I found another post that helped and there was a comment but to now expand and explain it correctly on this post:

This would have been our base domain class but instead of creating it in grails-app/domains/ it is created under src/main/groovy/test

package test

abstract class Tester {
    String name
    String something

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

Next out actual domain classes in grails-app/domains/test:

package  test
class Tester1 extends Tester {
}

and

package  test
class Tester2 extends Tester {
}

Looking at 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)

这篇关于相当于Grails中的InheritanceType.TABLE_PER_CLASS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆