Grails Bypass beforeUpdate Domain类的方法

时间:2013-03-20 17:26:57

标签: grails grails-2.0 grails-domain-class

我很好奇是否有办法绕过某个域类的beforeUpdate()和类似方法。

我需要它来恢复用户的原始密码。 beforeUpdate继续使用密码哈希算法,我不想再调用两次。

我能想到的唯一解决方案是使用直接连接到数据库,因此绕过Hibernate。我不喜欢数据库供应商锁定等原因。

感谢您的任何建议。

2 个答案:

答案 0 :(得分:1)

为什么不在域上放置一个ishashed属性....

设置新密码时将其设为false

在beforeUpdate中,你测试了ishashed

如果为false,则对密码进行散列并将ishashed设置为true

答案 1 :(得分:1)

您可以使用HQL:

User.executeUpdate("update User u set u.password = :password where u.id = :userId",
    [password: oldHashedPassword, userId: userId])

这仍然使用Hibernate及其来自数据源供应商的抽象。