我仍在学习Grails。我正在逐步建立我的启动项目。请原谅我这么多新手问题。
命令generate-all创建我的图书服务类。 Grails生成BookService。看起来像这样。
import grails.gorm.services.Service
@Service(Book)
interface BookService {
Book get(Serializable id)
List<Book> list(Map args)
Long count()
void delete(Serializable id)
Book save(Book book)
}
Grails使用保存操作生成BookController,该保存操作调用该服务以保存我的书。
bookService.save(book)
到目前为止一切顺利。我可以保存,没有任何问题。但是,我在保存操作中仅用 book.save()替换了bookService.save(book)。现在,它不会将我的书保存到数据库中。我也尝试 book.save(flush:true)。它也不会保存书。
您是否知道为什么book.save()(带有或不带有刷新:true)将不保存,而bookService.save(book)将保存?
我不知道Grails中接口BookService 的含义。 您能教我在哪里可以向BookService添加更多方法吗?
非常感谢。
答案 0 :(得分:0)
不允许保存外部交易。控制器不是事务性的。如果要保存在控制器中,请在事务中移动保存逻辑,例如-
Book.withTransaction { status ->
...
}
https://github.com/hibernate/hibernate-orm/blob/5.2/migration-guide.adoc#misc
最佳做法是将Service
层用于所有与数据库相关的活动。
大约interface Service
,请看http://gorm.grails.org/latest/hibernate/manual/index.html#dataServices
Grails: How to override generated service method?
Grails 3.3.3 generate-all <domain class> Creates only the Service Interface