【问题标题】:GORM inheritance issueGORM继承问题
【发布时间】:2010-06-26 00:06:49
【问题描述】:

我在这个 GORM 继承问题上被阻止了,我很感激一些新的眼睛来看看这个问题。 (我使用 Grails 1.3.2)

我有一个基础抽象类...

abstract class MaintenanceSchedule {
    static belongsTo = [ maintenanceTask:MaintenanceTask ]
}

我想像这样扩展它......

class OneOffSchedule extends MaintenanceSchedule {

    Date scheduleDate

    static constraints = {
        scheduleDate(nullable:false)
    }

    private static OneOffSchedule getReferenceOneOffSchedule() {
        return new OneOffSchedule(scheduleDate:new Date())
    }
}

当我尝试运行我的应用程序时,我得到以下堆栈跟踪。

2010-06-26 12:01:43,090 [main] ERROR context.GrailsContextLoader  - Error executing bootstraps: Error creating bean with name 'messageSource': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is org.hibernate.MappingException: An association from the table maintenance_task refers to an unmapped class: preventIT.maintenance.MaintenanceSchedule
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'messageSource': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is org.hibernate.MappingException: An association from the table maintenance_task refers to an unmapped class: preventIT.maintenance.MaintenanceSchedule
        at org.grails.tomcat.TomcatServer.start(TomcatServer.groovy:164)
        at grails.web.container.EmbeddableServer$start.call(Unknown Source)
        at _GrailsRun_groovy$_run_closure5_closure12.doCall(_GrailsRun_groovy:159)
        at _GrailsRun_groovy$_run_closure5_closure12.doCall(_GrailsRun_groovy)
        at _GrailsSettings_groovy$_run_closure10.doCall(_GrailsSettings_groovy:282)
        at _GrailsSettings_groovy$_run_closure10.call(_GrailsSettings_groovy)
        at _GrailsRun_groovy$_run_closure5.doCall(_GrailsRun_groovy:150)
        at _GrailsRun_groovy$_run_closure5.call(_GrailsRun_groovy)
        at _GrailsRun_groovy.runInline(_GrailsRun_groovy:116)
        at _GrailsRun_groovy.this$4$runInline(_GrailsRun_groovy)
        at _GrailsRun_groovy$_run_closure1.doCall(_GrailsRun_groovy:59)
        at RunApp$_run_closure1.doCall(RunApp.groovy:33)
        at gant.Gant$_dispatch_closure5.doCall(Gant.groovy:381)
        at gant.Gant$_dispatch_closure7.doCall(Gant.groovy:415)
        at gant.Gant$_dispatch_closure7.doCall(Gant.groovy)
        at gant.Gant.withBuildListeners(Gant.groovy:427)
        at gant.Gant.this$2$withBuildListeners(Gant.groovy)
        at gant.Gant$this$2$withBuildListeners.callCurrent(Unknown Source)
        at gant.Gant.dispatch(Gant.groovy:415)
        at gant.Gant.this$2$dispatch(Gant.groovy)
        at gant.Gant.invokeMethod(Gant.groovy)
        at gant.Gant.executeTargets(Gant.groovy:590)
        at gant.Gant.executeTargets(Gant.groovy:589)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is org.hibernate.MappingException: An association from the table maintenance_task refers to an unmapped class: preventIT.maintenance.MaintenanceSchedule
        ... 23 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is org.hibernate.MappingException: An association from the table maintenance_task refers to an unmapped class: preventIT.maintenance.MaintenanceSchedule
        ... 23 more
Caused by: org.hibernate.MappingException: An association from the table maintenance_task refers to an unmapped class: preventIT.maintenance.MaintenanceSchedule
        ... 23 more

【问题讨论】:

    标签: orm grails groovy grails-orm


    【解决方案1】:

    这个问题可以说是 gorm 中的一个 bug(可能是 hibernate,不确定 bug 的根源在哪里),因为它不会持久化抽象类并破坏多态关系。大概你有一个

    static hasMany = [schedules:MaintenanceSchedule]
    

    在您的 MaintenanceTask 类中。如果您的 MaintenanceSchedule 类真的就像它出现在您的帖子中一样,您可以通过删除该类上的抽象描述符来解决该问题。如果您想阻止某人创建 MaintenanceSchedule,您可以显式添加受保护的无参数构造函数。

    如果您想在 MaintenanceSchedule 中使用抽象方法,那么该方法就不能很好地工作。但是,如果您删除 abstract 关键字或 MaintenanceTask 上的关系,您的编译器错误应该会消失。

    【讨论】:

    • 谢谢。我现在正在使用你上面的建议加上一个接口来描述调度类的命令方法。
    【解决方案2】:

    在 Grails 1.4 版中,这将得到解决。其实已经解决了……不过要等到1.4稳定版发布了。

    http://jira.grails.org/browse/GRAILS-2583

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-05
      • 1970-01-01
      相关资源
      最近更新 更多