5

在我的环境中,我在 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

在 q 我明白了,当刷新休眠会话时会出现问题,休眠尝试再次保存对象然后抛出异常......

当再次尝试保存对象时,再次调用 book.validate(),在数据库中进行新的查询,以确保 email 字段的唯一性。现在,抛出了验证异常。

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

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

这是样例工程,模拟报错的步骤是:

  • 来源:https ://github.com/roalcantara/grails_app_validation_exception
  • grails 运行应用程序
  • 导航到 http://localhost:8080/book/book/create
  • 创建一个填充所有字段的新实例..
  • 然后编辑这个实例,在:http://localhost:8080/book/book/edit/1
  • 最后,删除“标题”字段并单击更新,然后抛出异常..

在我的环境中,这种行为发生在 grails 版本 2.0.3 和 2.2.1

谢谢你的帮助!对不起,我可怜的(和羞耻的)英语.. rs..

4

1 回答 1

6

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

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
    }
于 2013-03-14T01:44:13.063 回答