【问题标题】:How Grails gorm will insert table per concrete class where each concrete class inherits an abstract classGrails gorm 如何为每个具体类插入表,其中每个具体类都继承一个抽象类
【发布时间】:2012-08-03 06:48:21
【问题描述】:

大家好,以下是情况 我有一个抽象类 AbstractProfile 和一个具体类 GoogleProfile

abstract class AbstractProfile  {
    .....
}

class GoogleProfile extends AbstractProfile { 

    ......
}

我正在使用 grails,但 gorm 没有为 google 个人资料插入表格 当前 gorm 只为 AbstractProfile 类插入表格 请帮助提前谢谢

【问题讨论】:

  • 我使用的grails版本是2.0.4

标签: grails grails-orm


【解决方案1】:

我做了一些挖掘,发现从grails 2.3 开始,您有以下映射选项:

tablePerConcreteClass true

似乎文档(即使是 2.4 版本)还没有对此进行更新。

【讨论】:

    【解决方案2】:

    你可以用这个:

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

    【讨论】:

    • 对这段代码加一点解释,对以后的读者会有帮助。
    • 为了避免创建 base_domain 表,我还需要 tablePerHierarchy: false。我发现使用 tablePerConcreteClass 而不是将基域类放在 src 中的优点是我可以索引基域类中的列。
    • @gabe 谢谢我在这里创建了一个答案,解释你的意思stackoverflow.com/questions/22447280/… - 作为其他人的参考点
    【解决方案3】:

    Grails 2.0 保留了抽象类。为了将单个表启用到扩展类,您需要指定:

    static mapping = {
            tablePerHierarchy       false
    }
    

    到抽象类。否则,整个层次结构将“存在”在同一个表中。

    【讨论】:

    • 静态映射 = { tablePerHierarchy:false } 你好 heikkim 我已将此映射插入到我的抽象类中,但结果仍然相同
    • 定义中有一个额外的冒号,位于“tablePerHierarchy”和“false”之间。
    • 感谢 heikkin 它对我有用,但现在当我想将 google 配置文件的实例保存到 db 时,会生成以下 |错误 2012-08-03 12:52:41,748 [pool-15-thread-2] ERROR hibernate.AssertionFailure - 发生断言失败(这可能表明 Hibernate 中存在错误,但更有可能是由于会话使用不安全)消息:com.test.demo.GoogleProfile 条目中的空 id(发生异常后不要刷新会话)
    • 在添加 tablePerHierarchy false 之前是否删除了旧的表结构?
    • 实际上在我用于开发的 Datasource.groovy 文件中,我正在使用 create-drop 并且每次服务器启动时都会重新创建数据库结构
    【解决方案4】:

    跟进 Михаил 的出色回答,有两件事对我有用。

    1. 将基础域类放入 src

    这对我来说效果最好,因为基类和子类的约束都被应用了。子类中没有空列对我的用例很重要。但是,似乎只使用了子类中的映射块。

    1. 使用tablePerConcreteClass true

    这里的一个优点是它允许在 BaseDomain 类中声明一个索引,该索引随后出现在每个子类表中,即同时使用来自基类和子类的映射块。似乎只使用了基类中的约束。

    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
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-24
      • 2016-12-01
      • 1970-01-01
      • 2012-11-24
      • 2017-10-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多