删除即使在服务中也不起作用? [英] delete is not working even in service?
问题描述
我有图书领域
package bookfinal
class Book {
String name
static constraints = {
}
}
Book Controller如下
Book Controller is as follows
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()在服务中工作,因为它是事务性的.我有什么想念的吗?感谢您的帮助.谢谢!
Now, when i click on delete it redirects to index but the record isn't deleted. Why is delete not working even within service? I am using Grails 3.3.2 version. When i do delete(flush:true) then it works but i was expecting that just delete() work in service since it is transactional. Am i missing anything? I appreciate any help. Thanks!
推荐答案
这是Grails3的更改之一.检查官方Grails3文档:
It's one of the Grails3 changes. Check official Grails3 docs:
save
方法通知持久性上下文应保存或更新实例.除非使用flush
参数,否则该对象将不会立即保留
The save
method informs the persistence context that an instance should be saved or updated. The object will not be persisted immediately unless the flush
argument is used
更多信息:
在早期版本的GORM中,刷新模式默认为AUTO.设置此设置后,无论是否存在事务,都将在每次查询之前刷新会话.
In previous versions of GORM the flush mode defaulted to AUTO. With this setting the session would be flushed with or without the presence of a transaction and before every query.
如果开发人员不能完全理解AUTO
的行为,那么在查询之前频繁刷新会话可能会导致意外的性能后果.
The frequent flushing of the session before queries can lead to unintended performance consequences if the behaviour of the AUTO
is not fully understood by the developer.
请记住,COMMIT
是GORM 6中新的默认刷新模式,它将在事务提交时刷新会话.您可以通过在应用程序配置(例如application.yml)中配置刷新模式来恢复以前的行为:
With this in mind COMMIT
is the new default flush mode in GORM 6, which will flush the session on transaction commit. You can restore the previous behaviour by configuring the flush mode in your application configuration (for example application.yml):
hibernate:
flush:
mode: AUTO
这篇关于删除即使在服务中也不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!