在圣杯中保存实例的指南是什么?
我正在做这样的事情:
def colorInstance = Color.findOrSaveByName(colorname)
if (colorInstance.startsWith("R")) {
colorInstance.isRColor = true;
}
我是否应该在是否块中调用colorInstance.save()
?为什么?
每当您的流程需要时,您都可以显式保存一个实例(就像您的示例一样)。
考虑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-andsimerities,请阅读有关 Hibernate Session
在Grails 2.3.4 colorInstance .save flush:true
在Grails 2.2.2 colorInstance.save(flush:true)