title: Hibernate saveOrUpdate is not persisting completely
body: <p>This Question is related with the code residing <a href="https://github.com/openmrs/openmrs-core/blob/master/api/src/main/java/org/openmrs/api/db/hibernate/HibernateUserDAO.java" rel="nofollow">here</a></p>
  
  <pre><code>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 &amp;&amp; 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());
      }
  </code></pre>
  
  <p>Hibernate Configurations for <a href="https://github.com/openmrs/openmrs-core/blob/master/api/src/main/resources/org/openmrs/api/db/hibernate/User.hbm.xml" rel="nofollow">User</a> And <a href="https://github.com/openmrs/openmrs-core/blob/master/api/src/main/resources/org/openmrs/api/db/hibernate/LoginCredential.hbm.xml" rel="nofollow">LoginCredentials</a></p>
  
  <ul>
  <li><strong>LoginCredentials</strong> consists fields that are related to authenticating a user.</li>
  <li><strong>User</strong> contains fields related to user details.</li>
  <li><strong>LoginCredentials</strong> &amp; <strong>User</strong> represents a single table.</li>
  <li>When creating a new user, the <strong>saveUser</strong> method is called and when
  <code>sessionFactory.getCurrentSession().saveOrUpdate(user);</code> is executed,
  user object gets an <strong>ID</strong></li>
  <li><p>Then the execution is passed to <strong>updateUserPassword</strong> to save the LoginCredentials and
  <code>LoginCredential credentials = getLoginCredential(changeForUser);</code> is executed. </p></li>
  <li><p>But <strong>credentials</strong> gets <code>null</code> and causes a null pointer exception.</p></li>
  <li><p>If the User object was saved correctly, the credentials object should
  get an object with the user id assigned to it and other fields empty.</p></li>
  <li><p>I tried checking for null and manually creating a LoginCredentials object and assigning the user id, but it didn't work. </p></li>
  <li><p>Is this a problem that is related to the use of <strong>@Transactional</strong>
  annotation?</p></li>
  </ul>