首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Grails域类与继承之间的关系

Grails域类与继承之间的关系
EN

Stack Overflow用户
提问于 2014-10-22 00:36:39
回答 1查看 501关注 0票数 1

我有下一节课。在src/groovy中,

代码语言:javascript
复制
class Profile {
    String firstName
    String middleName
    String lastName
    byte[] photo
    String bio
}

域类BasicProfileAcademicProfile扩展Profile

代码语言:javascript
复制
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类。

代码语言:javascript
复制
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类。

代码语言:javascript
复制
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都可能有一个Profile ( BasicProfileAcademicProfile)。我尝试过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。Google搜索告诉我,这是继承其他类的类的问题。因此,从技术上讲,如果我在Publication中没有PublicationAcademicProfile的关系,那么它应该没有任何问题。但我不想那样。因为一个出版物有许多作者(在我的例子中是AcademicProfile),而且一个作者可能有很多出版物。有办法解决这个问题吗?
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-22 03:04:12

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

我不知道它为什么要抱怨AcademicProfile,但它可能是由核心问题引起的第二个bug。

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

如果您将Profile移动到grails-app/domain,它应该会工作。一旦这样做,您应该将tablePerSubclass映射配置移动到基类,并只指定一次。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26498631

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档