Grails域类与继承之间的关系

Grails域类与继承之间的关系,grails,inheritance,gorm,Grails,Inheritance,Gorm,我有以下课程。在src/groovy中 class Profile { String firstName String middleName String lastName byte[] photo String bio } 域类BasicProfile和AcademicProfile扩展Profile class BasicProfile extends Profile { User user Date dateCreated

我有以下课程。在
src/groovy

class Profile {
    String firstName
    String middleName
    String lastName
    byte[] photo
    String bio
}
域类
BasicProfile
AcademicProfile
扩展
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
    }
}
然后是一个
出版物

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
    }
}
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
    }
}
最后,我有一个
User

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
    }
}
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
    }
}
我的问题如下

  • 我想要一种关系,其中每个
    用户
    可以有一个
    配置文件
    (或者
    基本配置文件
    或者
    配置文件
    )。我尝试了
    static hasOne=[profile:profile]
    ,但我得到了错误,说
    profile
    不符合
    hasOne
    关系。因此,我目前的设置是一种变通方法。用户是否无法拥有一个
    配置文件
    ,无论是
    基本配置文件
    还是
    学术配置文件
  • 其次,在当前设置中,我得到错误:
    调用init方法失败;嵌套异常是org.hibernate.MappingException:当我尝试运行它时,来自表academic_profile_publications的关联引用了一个未映射的类:org.academical.academicrofile
    。谷歌搜索告诉我,这是继承自其他类的类的问题。因此,从技术上讲,如果我在
    出版物
    中没有与
    学术档案
    关系,那么它应该可以正常工作。但我不想那样。因为一份出版物有许多作者(在我的例子中是
    s),而一名作者可能有许多出版物。有没有办法解决这个问题

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

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

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


    如果您将
    Profile
    移动到
    grailsapp/domain
    ,它应该可以工作。一旦您这样做了,您应该将
    tablePerSubclass
    映射配置移动到基类,并且只指定它一次。

    我按照您的建议进行了操作,它成功了。现在,我正在尝试建立一对一关系,其中
    User
    BasicProfile
    是所有者,另一对一关系是
    AcademicProfile
    User
    其中
    AcademicProfile
    是所有者。我从中的
    hasOne
    中删除了
    academicrofile
    ,并将其添加为
    User
    中的一个字段。我将
    hasOne
    添加到
    academicrofile
    。但它失败了,错误是:
    -字段'academicProfile'上的对象'org.academical.User'中的字段错误:拒绝值[null]。你知道怎么了吗?你应该在另一个问题中问这个问题,我会问的。谢谢以下是作为单独问题的查询: