何时以grails保存实例?

时间:2014-02-04 16:41:25

标签: hibernate grails gorm

将实例保存在grails中有哪些指导原则?

我正在做这样的事情:

def colorInstance = Color.findOrSaveByName(colorname)
if (colorInstance.startsWith("R")) {
   colorInstance.isRColor = true;
}

我是否应该在if块中调用colorInstance.save()?为什么?

2 个答案:

答案 0 :(得分:1)

只要您的流程需要,您就可以显式保存实例(就像您的示例一样)。

考虑at the end of a request cycle, when a GORM session is flushed, a dirty check operation is performed. Objects that are binded to the session are compared against their original values and if changes are detected, they are persisted

http://www.redwindsw.com/blog/2014-01-15-moving-from-rails-to-grails-differences-and-similarities开始,阅读有关 Hibernate会话的段落。

答案 1 :(得分:0)

grails 2.3.4 colorInstance .save flush:true

in grails 2.2.2 colorInstance.save(flush:true)