Inheritance Grails继承映射中的鉴别器问题

Inheritance Grails继承映射中的鉴别器问题,inheritance,grails,table-per-hierarchy,discriminator,Inheritance,Grails,Table Per Hierarchy,Discriminator,我有4个类A、B、B1、B2,继承映射描述如下: A(映射到表A)是最顶层的父类,其继承映射策略为tablePerHierarchy=false(表示其每个子类都映射到一个表)。 类B从A扩展而来,B1、B2从B扩展而来。但B1和B2与B共享同一个表,因此在B类中,我声明tablePerHierarchy=true和一个分隔B1和B2的鉴别器。 详细代码如下: class A { static mapping = { tablePerHierarchy false

我有4个类A、B、B1、B2,继承映射描述如下:
A(映射到表A)是最顶层的父类,其继承映射策略为
tablePerHierarchy=false
(表示其每个子类都映射到一个表)。
类B从A扩展而来,B1、B2从B扩展而来。但B1和B2与B共享同一个表,因此在B类中,我声明
tablePerHierarchy=true
和一个分隔B1和B2的鉴别器。
详细代码如下:

class A {
     static mapping = {
          tablePerHierarchy false
          table 'A'
     }
}

class B extends A {
     static mapping = {
          tablePerHierarchy true
          table 'B'
          discriminator column : 'TYPE'
     }
     String bProp1
     String bProp2
}

class B1 extends B {
     static mapping = {
          discriminator column : 'TYPE', value : 'B1'
     }
     String b1Props1 
     String b1Props2
}

class B2 extends B {
     static mapping = {
          discriminator column : 'TYPE', value : 'B2'
     }
     String b2Props1
     String b2Props1
}
启动我的应用程序时,出现了一个错误:

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: No discriminator found for edu.test.B1. Discriminator is needed when 'single-table-per-hierarchy' is used and a class has subclasses
    at org.grails.tomcat.TomcatServer.start(TomcatServer.groovy:212)
    at grails.web.container.EmbeddableServer$start.call(Unknown Source)
    at _GrailsRun_groovy$_run_closure5_closure12.doCall(_GrailsRun_groovy:158)
    at _GrailsRun_groovy$_run_closure5_closure12.doCall(_GrailsRun_groovy)
    at _GrailsSettings_groovy$_run_closure10.doCall(_GrailsSettings_groovy:280)
    at _GrailsSettings_groovy$_run_closure10.call(_GrailsSettings_groovy)
    at _GrailsRun_groovy$_run_closure5.doCall(_GrailsRun_groovy:149)
    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: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: No discriminator found for edu.test.B1. Discriminator is needed when 'single-table-per-hierarchy' is used and a class has subclasses
    ... 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: No discriminator found for edu.test.B1. Discriminator is needed when 'single-table-per-hierarchy' is used and a class has subclasses
    ... 23 more
Caused by: org.hibernate.MappingException: No discriminator found for edu.test.B1. Discriminator is needed when 'single-table-per-hierarchy' is used and a class has subclasses
    ... 23 more
我遵循了Grails文档中关于discriminator()的说明,但它不起作用。我在这里找到了一个关于这个文档问题的JIRA:并在他们评论时修复了我的代码,但仍然得到了相同的错误

我不知道引起这个问题的原因。B中的tablePerHierarchy=true是否覆盖A中定义的策略?

你能帮我解决这个问题吗?非常感谢。

似乎并不真正支持为类层次结构混合继承策略,即使是JPA/Hibernate级别。 我认为您的问题与以下问题相同(或至少非常相似):


如果将“org.codehaus.groovy.grails.orm.hibernate”的日志记录设置更改为“debug”,则可以看到类B中的“tablePerHierarchy=true”设置不会覆盖A中的策略。无论如何,它都不起作用。同样,这看起来是一个比GORM更低的限制。可以修改您的设计以对该层次结构使用一种继承策略吗

你应该标出已接受的答案