【问题标题】:Relationships between Grails domain classes with inheritanceGrails 域类与继承之间的关系
【发布时间】:2014-10-22 00:36:39
【问题描述】:

我有以下课程。在src/groovy

class Profile {
    String firstName
    String middleName
    String lastName
    byte[] photo
    String bio
}

域类BasicProfileAcademicProfile 扩展Profile

class BasicProfile extends Profile {

    User user
    Date dateCreated
    Date lastUpdated

    static constraints = {
        firstName blank: false
        middleName nullable: true
        lastName blank: false
        photo nullable: true, maxSize: 2 * 1024**2
        bio nullable: true, maxSize: 500
    }

    static mapping = {
        tablePerSubclass true
    }
}

class AcademicProfile extends Profile {

    User user
    String dblpId
    String scholarId
    String website
    Date dateCreated
    Date lastUpdated

    static hasMany = [publications: Publication]

    static constraints = {
        importFrom BasicProfile
        dblpId nullable: true
        scholarId nullable: true
        website nullable: true, url: true
        publications nullable: true
    }

    static mapping = { 
        tablePerSubclass true
    }
}

然后有一个Publication 类。

class Publication {

    String dblpId
    String scholarId
    String title
    String description
    Date publicationDate
    int citations
    Date dateCreated
    Date lastUpdated

    static belongsTo = [AcademicProfile]
    static hasOne = [publisher: Publisher]
    static hasMany = [academicProfiles: AcademicProfile]

    static constraints = {
        dblpId nullable: true
        scholarId nullable: true
        title blank: false, maxSize: 100
        description nullable: true, maxSize: 500
        publicationDate: nullable: true
        academicProfiles nullable: false
    }
}

最后,我有一个User 类。

class User {

    String username
    String password
    String email
    Date dateCreated
    Date lastUpdated

    static hasOne = [basicProfile: BasicProfile, academicProfile: AcademicProfile]

    static constraints = {
        username size: 3..20, unique: true, nullable: false, validator: { _username ->
            _username.toLowerCase() == _username
        }
        password size: 6..100, nullable: false, validator: { _password, user ->
            _password != user.username
        }
        email email: true, blank: false
        basicProfile nullable: true
        academicProfile nullable: true
    }
}

我的问题如下。

  1. 我想要一个关系,其中每个User 都可以有一个ProfileBasicProfileAcademicProfile)。我尝试了static hasOne = [profile: Profile],但我收到错误说Profile 不同意hasOne 关系。所以我目前的设置是一种解决方法。用户有没有办法拥有一个Profile,无论是BasicProfile 还是AcademicProfile
  2. 其次,在当前设置中,当我尝试运行它时出现错误:Invocation of init method failed; nested exception is org.hibernate.MappingException: An association from the table academic_profile_publications refers to an unmapped class: org.academic.AcademicProfile。谷歌搜索告诉我,这是从其他类继承的类的问题。所以从技术上讲,如果我在PublicationAcademicProfile 中没有hasMany 关系,它应该可以正常工作。但我不想那样。因为publication 有很多authors(在我的例子中是AcademicProfiles),而author 可能有很多出版物。那么有没有办法解决这个问题?

【问题讨论】:

    标签: grails inheritance grails-orm


    【解决方案1】:

    您没有使用 Hibernate 继承 - 这需要映射所有类。您只是使用常规的 Java/Groovy 继承,从基类继承属性和方法。但是 Hibernate 并没有意识到这一点,所以它不能对未映射的基类进行查询。

    我不确定它为什么抱怨AcademicProfile,但这可能是核心问题引起的次要错误。

    我发现在大多数情况下使用 Hibernate 继承太令人沮丧,所以当有共享代码时我会使用这种方法。

    如果您将Profile 移动到grails-app/domain,它应该可以工作。完成此操作后,您应该将 tablePerSubclass 映射配置移动到基类,并且只指定一次。

    【讨论】:

    • 我听从了你的建议,它奏效了。现在我正在尝试建立一对一的关系 b/w UserBasicProfile 其中 User 是所有者和另一个一对一的关系 b/w AcademicProfileUser 其中 AcademicProfile 是所有者。我从hasOne 中删除了academicProfile,并将其作为字段添加到User 中。我将hasOne 添加到AcademicProfile。但它因错误而失败:- Field error in object 'org.academic.User' on field 'academicProfile': rejected value [null];。知道有什么问题吗?
    • 你应该在一个单独的问题中提出这个问题
    • 我会这样做的。谢谢!
    • 这里是一个单独的问题:stackoverflow.com/questions/26512671
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-27
    相关资源
    最近更新 更多