开发者

Discriminator issue in inheritance mapping of Grails

I have 4 classes A, B, B1, B2 with inheritance mapping described as below:

A (mapped to table A) is the top-most parent class and its inheritance mapping strategy is tablePerHierarchy=false (means each of its sub-classes is mapped to a single table).

Class B extends from A, and B1, B2 extends from B. But B1 and B2 share the same table with B, so in B class, I declare tablePerHierarchy=true and a discriminator to separate B1 and B2.

The detail code is here:

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
}

When I start my app, there's an error occured:

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_groo开发者_如何学运维vy: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

I have followed the instruction of Grails documentation about discriminator ( http://grails.org/doc/latest/ref/Database%20Mapping/discriminator.html ) but it did not work. I have found a JIRA about this document issue here: http://jira.grails.org/browse/GRAILS-5168 and fixed my code as they comment but it still got the same error.

I do not know the reason which caused this issue. Does the tablePerHierarchy=true in B override that strategy defined in A?

Could you please help me to fix this problem? Thank you so much.


It seems that mixing inheritance strategies for a class hierarchy is not really supported, even down to the JPA/Hibernate level. I think your issue is the same as (or at least very similar to) these:

Changing the inheritance strategy in branches of the class hierarchy via JPA annotations

How to mix inheritance strategies with JPA annotations and Hibernate?

If you change the logging settings for 'org.codehaus.groovy.grails.orm.hibernate' to 'debug' you can see that the 'tablePerHierarchy=true' setting in class B does NOT override the strategy in A. Regardless, it doesn't work. Again, it looks to be a limitation at a lower level than GORM. Can revise your design to use one inheritance strategy for that hierarchy?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜