I have a user repository which is declared like this
public interface UserRepository extends JpaRepository<ApplicationUser, Long>
In one of the services, I want to get the user and before returning, I want to change the password to dummy text, but I don't want to save that entity.
ApplicationUser user = userRepository.findByUsername(username);
user.setPassword("CENSORED");
return user;
With entity manager I can do getReference
and get a copy, but how do I do it with JPARepository?
Should I get the object with findByUsername()
and then use its id with getOne()
function?
ApplicationUser user = userRepository.findByUsername(username);
ApplicationUser user2 = userRepository.getOne(user.getId());
user2.setPassword("CENSORED");
return user;