I have a User entity which has all the user's data including the password hash. I also have an API getUserDetails which performs a simple userRepository.findById(id) to get all the user's data. However, in the process it also returns the password hash which I don't want to.
One way to remove the password hash is to remove it from the service layer after all the data has been fetched. Like this -
fun getUserDetails(id: Long): Optional<User> {
        val user: Optional<User> = userRepository.findById(id)
        user.get().password = ""
        return user
    }
The output will be like this -
{
  "id": 4,
  "roles": "ADMIN",
  "userName": "user3",
  "emailId": "hello@outlook.com",
  "password": "",
  "phoneNumber": "1234",
  "organisationName": "test",
  "isActive": true
}
The password value has been removed but the key still exists. I would like to get rid of that too.
Is there any other way which is more baked into JPA which I can use to achieve the said result?
 
     
    