更新时ValidationException:在AbstractPersistenceEventListener上刷新实体时发生验证错误

更新时ValidationException:在AbstractPersistenceEventListener上刷新实体时发生验证错误,validation,grails,gorm,failonerror,Validation,Grails,Gorm,Failonerror,在我的环境中,Config.groovy上有grails.gorm.failOnError=true package org.example class Book { String title String author String email static constraints = { title nullable: false, blank: false email nullable: false, blank: fa

在我的环境中,Config.groovy上有grails.gorm.failOnError=true

package org.example

class Book {

    String title
    String author
    String email

    static constraints = {
        title nullable: false, blank: false
        email nullable: false, blank: false, unique: true //apparently this is the problem..
    }
}
在控制器上,我有:

package org.example

class BookController {

def update() {

        def bookInstance = Book.get(params.id)
        if (!bookInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'book.label', default: 'Book'), params.id])
            redirect(action: "list")
            return
        }

        if (params.version) {
            def version = params.version.toLong()
            if (bookInstance.version > version) {
                bookInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'book.label', default: 'Book')] as Object[],
                          "Another user has updated this Book while you were editing")
                render(view: "edit", model: [bookInstance: bookInstance])
                return
            }
        }

        bookInstance.properties = params

        bookInstance.validate()

        if(bookInstance.hasErrors()) {

            render(view: "edit", model: [bookInstance: bookInstance])

        } else {

            bookInstance.save(flush: true)
            flash.message = message(code: 'default.updated.message', args: [message(code: 'book.label', default: 'Book'), bookInstance.id])
            redirect(action: "show", id: bookInstance.id)
        }      
    }
}
为了省钱,没关系。但是,在不设置标题字段的情况下进行更新时,我会得到:

Message: Validation error whilst flushing entity [org.example.Book]:
- Field error in object 'org.example.Book' on field 'title': rejected value []; codes [org.example.Book.title.blank.error.org.example.Book.title,org.example.Book.title.blank.error.title,org.example.Book.title.blank.error.java.lang.String,org.example.Book.title.blank.error,book.title.blank.error.org.example.Book.title,book.title.blank.error.title,book.title.blank.error.java.lang.String,book.title.blank.error,org.example.Book.title.blank.org.example.Book.title,org.example.Book.title.blank.title,org.example.Book.title.blank.java.lang.String,org.example.Book.title.blank,book.title.blank.org.example.Book.title,book.title.blank.title,book.title.blank.java.lang.String,book.title.blank,blank.org.example.Book.title,blank.title,blank.java.lang.String,blank]; arguments [title,class org.example.Book]; default message [Property [{0}] of class [{1}] cannot be blank]

   Line | Method
->>  46 | onApplicationEvent in org.grails.datastore.mapping.engine.event.AbstractPersistenceEventListener
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   895 | runTask            in java.util.concurrent.ThreadPoolExecutor$Worker
|   918 | run . . . . . . .  in     ''
^   680 | run                in java.lang.Thread
据我所知,问题发生在刷新hibernate会话时,hibernate再次尝试保存对象,然后引发异常

再次尝试保存对象时,再次调用book.validate(),它在数据库中进行新查询以确保电子邮件字段的唯一性。现在,将引发验证异常

但是,当我删除电子邮件的唯一验证属性时,更新会正常执行

我的问题是:这种行为正确吗?Hibernate会自动调用book.save吗

这是示例项目,模拟错误的步骤如下:

  • 资料来源:
  • grails run应用程序
  • 导航到http://localhost:8080/book/book/create
  • 创建一个填充所有字段的新实例
  • 然后在http://localhost:8080/book/book/edit/1中编辑此实例
  • 最后,删除“Title”字段并单击Update,然后抛出异常
在我的环境中,grails版本2.0.3和2.2.1上出现了这种行为


谢谢你的帮助!对不起,我的英语很差。。rs..

您实际上验证了两次,首先是:

bookInstance.validate()
其次是:

bookInstance.save(flush: true)
调用
bookInstance.save(flush:true)
a
boolean
时返回。Grails在生成控制器时默认会利用这一点,但由于某种原因,您似乎已经更改了默认生成的控制器Grails

只要替换这个:

    bookInstance.validate()

    if(bookInstance.hasErrors()) {

        render(view: "edit", model: [bookInstance: bookInstance])

    } else {

        bookInstance.save(flush: true)
        flash.message = message(code: 'default.updated.message', args: [message(code: 'book.label', default: 'Book'), bookInstance.id])
        redirect(action: "show", id: bookInstance.id)
    }  
为此:

    if( !bookInstance.save( flush: true ) ) {
        render(view: "edit", model: [bookInstance: bookInstance])
        return
    }