我有一个GORM对象,我正在进行集成测试.它有一个beforeUpdate
钩子,可以保存以前密码哈希的历史记录.代码看起来像这样:
class Credentials { List passwordHistory = [] String username String password static hasMany = [passwordHistory : String] def beforeUpdate() { // of course I'm not really storing plain-text passwords, but // this is just for illustration. if (isDirty('password')) { passwordHistory << password } } }
在集成测试中,我想知道为什么:
appUser.credentials.password = newPassword appUser.save(flush: true) sessionFactory.currentSession.flush() AppUser.withNewSession { appUser = AppUser.get(appUser.id) appUser.credentials.empty // eagerly fetch the list while session is open } assert !appUser.credentials.passwordHistory.empty() // contains the previous password
有效,但是
appUser.credentials.password = newPassword appUser.save() sessionFactory.currentSession.flush() AppUser.withNewSession { appUser = AppUser.get(appUser.id) appUser.credentials.empty // eagerly fetch the list while session is open } assert !appUser.credentials.passwordHistory.empty() // is empty
才不是.不同之处flush: true
在于appUser.save()
通话中.我认为调用save()
将对象附加到当前会话,但刷新当前会话不会将密码添加到passwordHistory
列表中.这里到底发生了什么?