【问题标题】:delete is not working even in service?删除即使在服务中也不起作用?
【发布时间】:2018-07-16 04:52:42
【问题描述】:

我有书域

package bookfinal

class Book {

    String name
    static constraints = {
    }
}

Book Controller如下

package bookfinal

class BookController {

    def bookService

    def destroy(Long id){
        bookService.destroy(id)
        redirect(action:'index')
    }

    def index() {

        [books: Book.findAll()]
    }

    def create(){

    }

    def save(){
        def book = new Book(params)
        book.save()
        redirect(action:'show', id:book.id)
    }

    def edit(Long id){
        [book: Book.get(id)]
    }

    def update(Long id){
        bookService.update(id, params)
        redirect(action:'index')
    }

    def show(Long id){
        [book: Book.get(id)]
    }


}

服务如下

package bookfinal

import grails.gorm.transactions.Transactional

@Transactional
class BookService {

    def destroy(Long id) {

        def b = Book.get(id)
        b.delete()

    }


    def update(Long id, params){

        def book = Book.get(id)
        book.properties = params
        book.save()
    }

}

显示页面如下

<h1> ${book.name} </h1>

<g:link action="edit" id="${book.id}">Edit</g:link>
<g:link action="destroy" id="${book.id}">Delete</g:link>
<g:link action="index">Back</g:link>

现在,当我单击删除时,它会重定向到索引,但记录不会被删除。为什么即使在服务中也无法删除?我正在使用 Grails 3.3.2 版本。当我执行 delete(flush:true) 时,它可以工作,但我希望 delete() 可以在服务中工作,因为它是事务性的。我错过了什么吗?我很感激任何帮助。谢谢!

【问题讨论】:

    标签: grails grails3


    【解决方案1】:

    这是 Grails3 的更改之一。检查official Grails3 docs

    save 方法通知持久性上下文应该保存或更新一个实例。除非使用 flush 参数,否则对象不会立即持久化

    More info:

    在以前的 GORM 版本中,刷新模式默认为 AUTO。使用此设置,无论是否存在事务以及在每次查询之前都会刷新会话。

    如果开发人员没有完全理解 AUTO 的行为,那么在查询之前频繁刷新会话可能会导致意外的性能后果。

    考虑到这一点,COMMIT 是 GORM 6 中新的默认刷新模式,它将在事务提交时刷新会话。您可以通过在应用程序配置(例如 application.yml)中配置刷新模式来恢复之前的行为:

    hibernate:
        flush:
            mode: AUTO
    

    【讨论】:

      猜你喜欢
      • 2014-10-08
      • 1970-01-01
      • 1970-01-01
      • 2021-07-14
      • 2016-11-06
      • 1970-01-01
      • 1970-01-01
      • 2021-07-08
      • 2021-04-23
      相关资源
      最近更新 更多