orm - GORM继承问题

标签 orm grails groovy grails-orm

我在这个 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

最佳答案

这个问题可以说是gorm的一个bug(可能是hibernate,不知道bug的根源在哪里)因为它不持久化抽象类并且破坏了多态关系。想必你有一个

static hasMany = [schedules:MaintenanceSchedule]

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

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

关于orm - GORM继承问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3122179/

相关文章:

php - 如何在magento中获取属性组

python - 如何在多个数据库和模式上重复使用 sqlalchemy ORM 模型?

grails - Grails映射对象

Java 流收集器在 Set::size 上出现 groovy 错误

php - 教义 findOneBy 方法不起作用

php - Laravel 关系中的按位和条件( Eloquent ORM)

validation - Grails commandObject - 设计最佳实践

grails - Grails:在运行时配置数据源

java - Groovy def 和 Java 对象之间的区别?

groovy - Groovy中使用的“?:”运算符是什么?