【问题标题】:Null ID after Grails save()Grails save() 后的空 ID
【发布时间】:2015-08-28 20:03:47
【问题描述】:

遇到一个奇怪的错误,并放弃了在谷歌上搜索解决我问题的答案。这是我的域名

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 脚本

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) 性质不是自动递增的?

【问题讨论】:

    标签: hibernate grails null save flush


    【解决方案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)
    }
    

    【讨论】:

    • 中提琴!有用。我在数据库方面遇到了一些小问题,但修复了它,现在可以使用了。谢谢一百万!
    猜你喜欢
    • 2017-12-31
    • 1970-01-01
    • 1970-01-01
    • 2012-03-07
    • 1970-01-01
    • 1970-01-01
    • 2019-08-13
    • 2017-06-12
    • 1970-01-01
    相关资源
    最近更新 更多