equivalent of InheritanceType.TABLE_PER_CLASS in g

2019-03-05 22:11发布

问题:

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?

回答1:

Try to use tablePerHierarchy false

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

class CreditCardPayment extends Payment {
    String cardNumber
}

See more : http://grails.org/doc/latest/guide/single.html#5.5.2.3%20Inheritance%20Strategies



回答2:

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)