User managedUser = this.updateUserData(user);
// Create history entry
- PasswordHistory entry = new UserPasswordHistory();
+ PasswordHistory entry = new UserPasswordHistory(managedUser.getUserEncryptedPassword(), managedUser);
- // Set all data here to avoid user instance become detached again
- entry.setUserPasswordHistoryPasswordHash(managedUser.getUserEncryptedPassword());
- entry.setUserPasswordHistoryUser(this.getManagedUser(user));
+ // Set created timestamp
entry.setUserPasswordHistoryCreated(new GregorianCalendar());
+ // Merge user to make sure it is not re-persisted
+ User mergedUser = this.getEntityManager().merge(managedUser);
+ entry.setUserPasswordHistoryUser(mergedUser);
+
// Persist it
this.getEntityManager().persist(entry);