将实例保存在grails中有哪些指导原则?
我正在做这样的事情:
def colorInstance = Color.findOrSaveByName(colorname)
if (colorInstance.startsWith("R")) {
colorInstance.isRColor = true;
}
我是否应该在if块中调用colorInstance.save()
?为什么?
答案 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)