此问题与驻留here
的代码有关public User saveUser(User user, String password) {
// this gets as true since this is a new user
boolean isNewUser = user.getUserId() == null;
sessionFactory.getCurrentSession().saveOrUpdate(user);
if (isNewUser && password != null) {
//update the new user with the password
String salt = Security.getRandomToken();
String hashedPassword = Security.encodeString(password + salt);
updateUserPassword(hashedPassword, salt, Context.getAuthenticatedUser().getUserId(), new Date(), user
.getUserId());
// the method is stated below
}
return user;
}
private void updateUserPassword(String newHashedPassword, String salt, Integer changedBy, Date dateChanged,
Integer userIdToChange) {
User changeForUser = getUser(userIdToChange);
if (changeForUser == null) {
throw new DAOException("Couldn't find user to set password for userId=" + userIdToChange);
}
User changedByUser = getUser(changedBy);
LoginCredential credentials = getLoginCredential(changeForUser);
// the above method is stated below
credentials.setUserId(userIdToChange);
credentials.setHashedPassword(newHashedPassword);
credentials.setSalt(salt);
credentials.setChangedBy(changedByUser);
credentials.setDateChanged(dateChanged);
credentials.setUuid(changeForUser.getUuid());
sessionFactory.getCurrentSession().merge(credentials);
// reset lockout
changeForUser.setUserProperty(OpenmrsConstants.USER_PROPERTY_LOCKOUT_TIMESTAMP, "");
changeForUser.setUserProperty(OpenmrsConstants.USER_PROPERTY_LOGIN_ATTEMPTS, "0");
saveUser(changeForUser, null);
}
public LoginCredential getLoginCredential(User user) {
return (LoginCredential) sessionFactory.getCurrentSession().get(LoginCredential.class, user.getUserId());
}
的休眠配置
sessionFactory.getCurrentSession().saveOrUpdate(user);
已执行,
用户对象获得 ID 然后执行传递给 updateUserPassword 以保存LoginCredentials和
LoginCredential credentials = getLoginCredential(changeForUser);
已执行。
但凭据获取null
并导致空指针异常。
如果User对象已正确保存,则凭据对象应该 获取一个分配给它的用户ID和其他字段为空的对象。
我尝试检查null并手动创建LoginCredentials对象并分配用户ID,但它不起作用。
这是一个与使用 @Transactional 相关的问题 注释