为什么当我刷新实体管理器时数据库没有更新?
我目前正在开发一个社交网站,并且正在实现用户可以更改密码的部分。我正在使用实体管理器使用新密码刷新数据库的内容。下面是实现的代码。
final Implementation user = em.find(Implementation.class, username);
if((user!=null) && user.getPassword().equals(hash(username,oldPassword))){
user.setPassword(hash(username,newPassword));
em.refresh(user);
}else{
throw new ChangePasswordException();
}
但是,当我尝试再次登录时,必须使用旧密码,否则,如果提供新密码,它会告诉您:密码不匹配。有谁知道为什么会发生这种情况?我尝试首先从数据库中删除用户,然后再次保留新用户。但是,由于用户未从数据库中删除,因此用户名不唯一,因此生成了 EJB 异常。
非常感谢您的帮助
I'm currently developing a social networking site and I'm currently implementing the part where a user can change his password. I'm using the entity manager to refresh the contents of the database with the new password. The following is the code for the implementation.
final Implementation user = em.find(Implementation.class, username);
if((user!=null) && user.getPassword().equals(hash(username,oldPassword))){
user.setPassword(hash(username,newPassword));
em.refresh(user);
}else{
throw new ChangePasswordException();
}
however when I try to login again, the older password must be used, otherwise, if the new password is supplied it will tell you: passwords do not match. Does anyone know maybe why this is happening? I tried to first remove the user from the database, and then persist the new user again. However an EJB Exception was generated as the username was not unique since the user was not removed from the database.
Thanks a lot for your help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有保存新密码。您正在覆盖所做的更改。因此,
refresh(user)
将获取该用户的当前状态并将其写入您的对象中。尝试使用
merge
或persist
代替You are not saving your new password. You are overwriting your changes you have made. So
refresh(user)
will fetch the current state of that user and will write it into your object.Try to use
merge
orpersist
instead