2013-10-26 71 views
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 
    } 
    ... 
} 

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

回答

2

以平齐的定义从the docs

如果设置为true的持久性上下文将被刷新导致 情况下被立即删除。

还有更多关于SO的this related question。相关的部分,你的问题是:

让Hibernate做的工作,只有手动刷新会话时 有,或者至少只在批量更新的结束。如果您在 应该在那里时没有看到数据库中的数据,您应该只使用 。我知道这有点不好意思,但是当这种行为是必要的情况下取决于数据库 的实施和其他因素。

这就是说,你可以让这个方法的调用者决定是否需要被刷新:

TreeNode removeFromChildren(TreeNode child, boolean flush = false) { 
    TreeNodeChild.findByNodeAndChild(this, child).delete(flush: flush) 
    this 
}