0

我想知道当我对域类中的数据库执行操作时是否必须设置 flush: true 。例如:

class TreeNode {    

    TreeNode removeFromChildren(TreeNode child) {
       TreeNodeChild.findByNodeAndChild(this, child).delete(flush: true)
       this
    }
    ...
}

或者以下是正确的表示法?

class TreeNode {    

    TreeNode removeFromChildren(TreeNode child) {
       TreeNodeChild.findByNodeAndChild(this, child).delete()
       this
    }
    ...
}

问题是:我应该刷新会话吗?

4

1 回答 1

2

文档中获取flush的定义:

如果设置为 true,则持久上下文将被刷新,从而导致实例被立即删除。

还有更多关于 SO的相关问题。与您的问题相关的部分是:

让 Hibernate 完成它的工作,只在需要时手动刷新会话,或者至少在一批更新结束时手动刷新会话。只有当数据库中的数据应该存在时,您才应该真正使用它。我知道这有点空想,但需要采取这种行动的情况取决于数据库实现和其他因素。

也就是说,您可以让此方法的调用者决定是否需要刷新它:

TreeNode removeFromChildren(TreeNode child, boolean flush = false) {
  TreeNodeChild.findByNodeAndChild(this, child).delete(flush: flush)
  this
}
于 2013-10-26T20:25:37.047 回答