Grails / Gorm:继承和外键

时间:2016-03-15 15:33:53

标签: 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 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]
}

至少在启动时并没有爆发,但仍然是:为什么?

1 个答案:

答案 0 :(得分:0)

您可以像这样照顾您的域类星座:

class User { }

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

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

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

这会在PatientMedicationPrescription之间以及PatientOtherStuff之间创建双向一对多http://www.lfd.uci.edu/~gohlke/pythonlibs/#shapely。请注意,每个belongsTo都有一个对应的hasMany

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