Tapan
Tapan

Reputation: 187

Partial update without retrieving the object from db

I am looking for some way to partially update an object without retrieving the object from DB. Say I have an Employee entity containing the following properties -

  1. firstName
  2. lastName
  3. phone
  4. age
  5. salary

The JSON I get in the update request may not contain all the properties. I need to make sure I update only the properties provided in the request and leave all other data unchanged.

I explored some ways of achieving partial update but all of them involves retrieving the data from db. I don't have this option since the db in my case is too slow and this will increase the response time. Please suggest

Upvotes: 0

Views: 358

Answers (1)

code_mechanic
code_mechanic

Reputation: 1148

You can combine @Modifying and @Query annotation to issue an update query

@Modifying
@Query("update Employee e set e.firstName = :firstName, e.lastName = :lastName where e.id = :id")
void updateEmployeePartially(Long id, String firstName, String lastName);

For more information, you can check this article

Upvotes: 1

Related Questions