Grails/Gorm:继承和外键

Grails/Gorm:继承和外键,grails,gorm,Grails,Gorm,给定如下域类: class A { // in reality this is a basic User class that is required in multiple projects } class B extends A { // in reality B is a "patient"-kind of user. static hasMany = [c: C] } // c/d is stuff like "MedicationPrescription", so b

给定如下域类:

class A {   // in reality this is a basic User class that is required in multiple projects
}

class B extends A { // in reality B is a "patient"-kind of user. 
    static hasMany = [c: C]
}

// c/d is stuff like "MedicationPrescription", so basically data only relevant to the patient. However the system needs to realize that Patients are Users, as the User base class is used for spring security logins and in general has a lot of the basic data a person just has. (Name, etc.)
class C {
    static belongsTo = [b: B, a: A, d: D]
}

class D {
}
我得到这个错误:

org.hibernate.MappingException: Foreign key (FK_pwu2w72ul5a5213husrv3onr3:c [])) must have same number of columns as the referenced primary key (a [id])
        at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:110)
        at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:93)
        at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:1818)
        at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1741)
        at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1426)
        at org.grails.orm.hibernate.cfg.HibernateMappingContextConfiguration.secondPassCompile(HibernateMappingContextConfiguration.java:287)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1846)
        at org.grails.orm.hibernate.cfg.HibernateMappingContextConfiguration.buildSessionFactory(HibernateMappingContextConfiguration.java:196)
        at org.grails.orm.hibernate.HibernateMappingContextSessionFactoryBean.doBuildSessionFactory(HibernateMappingContextSessionFactoryBean.java:476)
        at org.grails.orm.hibernate.HibernateMappingContextSessionFactoryBean.buildSessionFactory(HibernateMappingContextSessionFactoryBean.java:470)
        at org.grails.orm.hibernate.HibernateMappingContextSessionFactoryBean.afterPropertiesSet(HibernateMappingContextSessionFactoryBean.java:93)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
        ... 41 common frames omitted
如果我删除A和B之间的继承,错误就会消失,但我的域中有这个星座。我不明白Gorm为什么这么做,从错误消息中,它似乎认为外键中的属性列表是空的

此外,我甚至不能在类A中提到类B、C或D,因为类A是插件的一部分,只是不知道这些类

编辑: 我可能会放弃继承,而是像这样使用组合:

class B {
  A a
  static hasMany = [c: C]
}
class User { }

class Patient extends User {
    static hasMany = [prescriptions: MedicationPrescription, stuff: OtherStuff]
}

class MedicationPrescription {
    static belongsTo = [patient: Patient]
}

class OtherStuff {
    static belongsTo = [patient: Patient]
}

这至少在启动时不会爆炸,但仍然是:为什么?

您可以像这样管理您的域类星座:

class B {
  A a
  static hasMany = [c: C]
}
class User { }

class Patient extends User {
    static hasMany = [prescriptions: MedicationPrescription, stuff: OtherStuff]
}

class MedicationPrescription {
    static belongsTo = [patient: Patient]
}

class OtherStuff {
    static belongsTo = [patient: Patient]
}
这将在
患者
药物处方
之间以及
患者
其他物品
之间创建双向一对多。请注意,每个
belongsTo
都有相应的
hasMany


正如您已经发现的,您也可以使用合成。

我怀疑问题在于您使用的是
belongsTo
而另一侧没有相应的
hasMany
。你能对你的类名进行反清理,使你想要达到的效果更加明显吗?增加了相关信息