Grails save() 后的空 ID
Null ID after Grails save()
遇到一个奇怪的错误并放弃了谷歌搜索来解决我的问题。这是我的域名
class UserTest {
User user
Integer testId
String testAgency
String testType
Date testDate
static mapping = {
table 'user_test'
id generator: 'identity', name: 'testId', type: 'long'
testId column: 'test_id'
version false
}
static constraints = {
testAgency(blank: true, nullable: true)
testType(blank: true, nullable: true)
testDate(blank: true, nullable: true)
}
}
这是我的 SQL 脚本,它创建了我的 table
CREATE TABLE user_test (
test_id int NOT NULL IDENTITY(1,1),
test_agency varchar(128) NULL,
test_type varchar(128) NULL,
test_date datetime NULL,
user_id int NULL foreign key references user_data(user_id),
PRIMARY KEY (test_id)
)
这是我在 UserController
中尝试保存的位置
def saveTest(String id) {
def user = User.findByUserId(id)
def test_to_add = new UserTest(testAgency:params.testAgency,
testType:params.testType,
testDate:params.testDate,
user:user)
user.addToTests(test_to_add)
user.save(flush:true, failOnError:true)
user.refresh()
redirect(action:'profile', id:user.userId)
}
但是我收到了错误
Class
org.hibernate.AssertionFailure
Message
null id in UserTest entry (don't flush the Session after an exception occurs)
突出显示 user.save(flush:true, failOnError:true)
行。我做了很多谷歌搜索,找不到任何有用的东西。关于为什么 testId
的 IDENTITY(1,1) 性质不是自动递增的,有没有人马上明白?
试试这个
def saveTest(String id) {
def user = User.findByUserId(id)
def test_to_add = new UserTest(testAgency:params.testAgency,
testType:params.testType,
testDate:params.testDate,
user:user).save(failOnError: true)
user.addToTests(test_to_add)
redirect(action:'profile', id:user.userId)
}
遇到一个奇怪的错误并放弃了谷歌搜索来解决我的问题。这是我的域名
class UserTest {
User user
Integer testId
String testAgency
String testType
Date testDate
static mapping = {
table 'user_test'
id generator: 'identity', name: 'testId', type: 'long'
testId column: 'test_id'
version false
}
static constraints = {
testAgency(blank: true, nullable: true)
testType(blank: true, nullable: true)
testDate(blank: true, nullable: true)
}
}
这是我的 SQL 脚本,它创建了我的 table
CREATE TABLE user_test (
test_id int NOT NULL IDENTITY(1,1),
test_agency varchar(128) NULL,
test_type varchar(128) NULL,
test_date datetime NULL,
user_id int NULL foreign key references user_data(user_id),
PRIMARY KEY (test_id)
)
这是我在 UserController
def saveTest(String id) {
def user = User.findByUserId(id)
def test_to_add = new UserTest(testAgency:params.testAgency,
testType:params.testType,
testDate:params.testDate,
user:user)
user.addToTests(test_to_add)
user.save(flush:true, failOnError:true)
user.refresh()
redirect(action:'profile', id:user.userId)
}
但是我收到了错误
Class
org.hibernate.AssertionFailure
Message
null id in UserTest entry (don't flush the Session after an exception occurs)
突出显示 user.save(flush:true, failOnError:true)
行。我做了很多谷歌搜索,找不到任何有用的东西。关于为什么 testId
的 IDENTITY(1,1) 性质不是自动递增的,有没有人马上明白?
试试这个
def saveTest(String id) {
def user = User.findByUserId(id)
def test_to_add = new UserTest(testAgency:params.testAgency,
testType:params.testType,
testDate:params.testDate,
user:user).save(failOnError: true)
user.addToTests(test_to_add)
redirect(action:'profile', id:user.userId)
}