我尝试创建一个SecUser
域对象,其中包含UserGroup
个对象的集合。每个UserGroup
都应该有一个SecUser
类型的创建者。 UserGroup
还包含SecUsers
的集合(例如,Facebook风格应用上的创建者朋友集合)。到目前为止,我有以下域类...
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
(因为它没有&{} #39; t得救了)。但有没有办法让addToUsergroups
操作在没有id
的情况下完成? (因为UserGroup
不应该在没有SecUser
的情况下创建。或者我的域对象的设计有问题吗?
提前致谢!
答案 0 :(得分:0)
我猜你的UserGroup表中的PK列叫做USER_GROUP_ID,如果这是你的所有代码,你就不会说任何关于它的内容,并且你在数据库中有一个约束,它不允许你把它留空。因此,您必须更改UserGroup id ,指定以不同方式调用 PK 列。
class UserGroup {
SecUser creator;
static belongsTo = SecUser;
static hasMany = [
members : SecUser
]
static mapping = {
id column: 'user_group_id'
}
static constraints = {
creator nullable: false
members nullable: true, maxSize: 100
}
}
希望它有所帮助。