How do I update an entity using spring-data-jpa?

To update an entity using Spring Data JPA, you will need to follow these steps:

  1. Retrieve the entity object that you want to update. You can do this using the findById() method of the JpaRepository interface.

  2. Modify the fields of the entity object as needed.

  3. Save the entity back to the database by calling the save() method of the JpaRepository interface.

Here's an example:

// Retrieve the entity object
Optional<User> optionalUser = userRepository.findById(1L);

if (optionalUser.isPresent()) {
    // Modify the fields of the entity object
    User user = optionalUser.get();
    user.setUsername("newUsername");
    user.setPassword("newPassword");
    
    // Save the entity
    userRepository.save(user);
}

Note that the save() method will perform an update if the entity already exists in the database, and an insert if it does not.

I hope this helps! Let me know if you have any other questions.