我试图创建一个SecUser
域对象,其中包含一组UserGroup
对象。每个UserGroup
应该有一个SecUser
类型的创建者。 A UserGroup
也包含SecUsers
(例如,创作者在Facebook风格应用程序中的朋友集合)的集合。到目前为止,我有以下域类...Grails域错误:列“USER_GROUP_ID”不允许为NULL
class SecUser implements Serializable {
static constraints = {
....
usergroups nullable: true
}
static mapping = {
...
usergroups cascade: 'all-delete-orphan'
}
static hasMany = [
...
usergroups: UserGroup
]
}
class UserGroup {
SecUser creator;
static belongsTo = SecUser;
static hasMany = [
members : SecUser
]
static constraints = {
creator nullable: false
members nullable: true, maxSize: 100
}
}
在Bootstrap.groovy
我尝试创建像这样一些SecUser
和UserGroup
对象...
def secuser = new SecUser(username: "username", password: "password");
def group1 = new UserGroup(title: "Friends", description: "friends");
def group3 = new UserGroup(title: "Colleagues", description: "colleagues");
secuser.addToUsergroups(group1);
secuser.addToUsergroups(group2);
但我发现了它说的错误...
ERROR spi.SqlExceptionHelper - NULL not allowed for column "USER_GROUP_ID"; SQL statement:
insert into sec_user_usergroups (sec_user_id, creator_id) values (?, ?) [23502-176]
Error |
2015-07-28 11:04:49,208 [localhost-startStop-1] ERROR context.GrailsContextLoaderListener - Error initializing the application: Hibernate operation: could not execute statement; SQL [n/a]; NULL not allowed for column "USER_GROUP_ID"; SQL statement:
insert into sec_user_usergroups (sec_user_id, creator_id) values (?, ?) [23502-176]; nested exception is org.h2.jdbc.JdbcSQLException: NULL not allowed for column "USER_GROUP_ID"; SQL statement:
insert into sec_user_usergroups (sec_user_id, creator_id) values (?, ?) [23502-176]
Message: Hibernate operation: could not execute statement; SQL [n/a]; NULL not allowed for column "USER_GROUP_ID"; SQL statement:
insert into sec_user_usergroups (sec_user_id, creator_id) values (?, ?) [23502-176]; nested exception is org.h2.jdbc.JdbcSQLException: NULL not allowed for column "USER_GROUP_ID"; SQL statement:
insert into sec_user_usergroups (sec_user_id, creator_id) values (?, ?) [23502-176]
我猜节目的告诉我,它无法完成addToUsergroups
操作,因为UserGroup
还没有得到一个id
(因为它还没有保存)。但有没有办法可以让addToUsergroups
操作在没有id
的情况下完成? (因为如果没有SecUser
,则不应创建UserGroup
)。或者,我的域对象的设计有问题吗?
在此先感谢!
这不起作用,但我找到了两种方法来解决它。我可以从'UserGroup'中删除'SecUser creator'字段,它可以工作,或者离开'creator'字段并将'static mappedBy = [usergroups:“creator”]'添加到'SecUser'对象。不知道为什么修复它,但我必须更多地阅读GORM。 – CSharp
另一件事,检查**静态belongsTo = SecUser **应该是** static belongsTo = [creator:SecUser] **而不是 – juandiegoh
是的,这工作太juandiegoh,谢谢。在我做出更改之前,它不会工作,除非它是'static belongsTo = SecUser',但现在没关系。 – CSharp