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]代码>。你知道怎么了吗?你应该在另一个问题中问这个问题,我会问的。谢谢以下是作为单独问题的查询: