【问题标题】:Grails, MySQL and extended classesGrails、MySQL 和扩展类
【发布时间】:2021-04-30 00:34:04
【问题描述】:

这是我的抽象类:

package mypackage.commons

abstract class Content {
    String name

    static constraints = {
        name nullable: false, blank: false, size: 1..50
    }
}

这是我的扩展类:

package mypackage.project

import mypackage.commons.Content

class Plane extends Content {
    String something;
}

这是我的 Bootstrap.groovy 文件:

package mypackage

import mypackage.commons.Content
import mypackage.project.Plane

class BootStrap {

    def init = { servletContext ->

        Plane boing1 = new Plane (name: 'Boing', something: 'Hello').save()
        Plane boing2 = new Plane (name: 'Boing', something: 'Goodbye').save()
        
    }
    def destroy = {
    }
}

问题是当我在 MySQL 上,当我使用 SHOW TABLES 时,我只能看到 content plane

这里是SELECT * FROM content;

的内容
+----+---------+-------+-------------------------+-----------+
| id | version | name  | class                   | something |
+----+---------+-------+-------------------------+-----------+
|  1 |       0 | Boing | mypackage.project.Plane | Hello     |
|  2 |       0 | Boing | mypackage.project.Plane | Goodbye   |
+----+---------+-------+-------------------------+-----------+

编辑

测试迈克的答案后:

package mypackage.commons

abstract class Content {
    String name

    static constraints = {
        name nullable: false, blank: false, size: 1..50
    }

    static mapping = {
        tablePerHierarchy false
    }
}

这是SHOW TABLES

的结果
+-----------------------+
| Tables_in_my_database |
+-----------------------+
|  content              |
|  plane                |
+-----------------------+

这是 SELECT * FROM content 的结果:

+----+---------+-------+
| id | version | name  |
+----+---------+-------+
|  1 |       0 | Boing |
|  2 |       0 | Boing |
+----+---------+-------+

这是 SELECT * FROM plane 的结果:

+----+------------+
| id |  something |
+----+------------+
|  1 |  Hello     |
|  2 |  Goodbye   |
+----+------------+

编辑结束

预期行为:

SHOW TABLES; 应该只显示表格 plane

SELECT * FROM plane 应该告诉我这个:

+----+---------+-------+------------+
| id | version | name  |  something |
+----+---------+-------+------------+
|  1 |       0 | Boing |  Hello     |
|  2 |       0 | Boing |  Goodbye   |
+----+---------+-------+------------+

我怎样才能得到预期的结果?

有可能吗?

提前致谢。

【问题讨论】:

    标签: mysql grails grails-orm abstract extends


    【解决方案1】:

    你需要告诉 Grails 使用 table-per-subclass 策略,告诉它不要使用 table-per-hierarchy 像:

    package mypackage.commons
    
    abstract class Content {
        String name
    
        static constraints = {
            name nullable: false, blank: false, size: 1..50
        }
    
        static mapping = {
            tablePerHierarchy false
        }
    }
    

    【讨论】:

    • 你好。感谢您的回答,但这不能按预期工作。我现在在“name”列中有带有“Boing”和“Boing”的“Content”表,在“something”列中有带有“Hello”和“GoodBye”的“plane”表。我想知道 Grails 如何在该配置中映射表,而无法达到预期的行为。
    • 我根据您的回答编辑了我的原始帖子。
    【解决方案2】:

    我找到了答案,我想知道这是否是最好的方法,但它确实有效。

    这个话题对我有帮助:https://github.com/grails/grails-data-mapping/issues/1254

    首先,我知道如果我不想让 Grails 映射一个类,我必须把这个类放在“域”包之外。

    所以我将我的 Content 类移动到 src -> main -> groovy -> common(我创建了这个文件夹) -> Content.groovy

    这是我的内容类:

    package common
    
    abstract class Content {
        String name
    
        static constraints = {
            name nullable: false, blank: false, size: 1..50
        }
    }
    

    这是我的飞机课:

    package mypackage.project
    
    import common.Content
    
    class Plane extends Content {
        String something;
    }
    

    这是我的 Bootstrap.groovy:

    package mypackage
    
    import mypackage.project.Plane
    
    class BootStrap {
    
        def init = { servletContext ->
    
            Plane boing1 = new Plane (name: 'Boing', something: 'Hello').save()
            Plane boing2 = new Plane (name: 'Boing', something: 'Goodbye').save()
            
        }
        def destroy = {
        }
    }
    

    所以我避免使用 tablePerHierarchy。

    在主题中,他谈到了@MappedSupperclass,我不知道它指的是什么。我没用过。

    达到预期的行为。

    这是正确的做法吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-26
      • 1970-01-01
      • 1970-01-01
      • 2016-01-15
      • 2017-03-11
      • 2016-01-03
      • 1970-01-01
      • 2020-07-22
      相关资源
      最近更新 更多