Arpit Joshi
Arpit Joshi

Reputation: 179

Issue with fetching updated data just after Update Spring Data Jpa

I am updating using @Modifying and just after update I am fetching data using the repository. But the data fetched is not the updated data but the data before update is done. When I fetch the same using other GET api then the updated data is fetched but not just after updating in the same method.

My Service class update method:

public Response update(Integer value, Long id) {

    repository.updateValue(value, id);
    Response res = repository.findById(id);
    return res;
}

My Repository class update method:

@Transactional
@Modifying
@Query(value = "update T1 set value = :value where id = :id", 
nativeQuery = true)
int updateValue(Integer value, Long id);

As mentioned I am getting the previous values in "res". Can anyone help with this issue?

Upvotes: 5

Views: 1752

Answers (1)

Arpit Joshi
Arpit Joshi

Reputation: 179

After adding clearAutomatically=true in @Modifying annotation the update worked just fine.

@Transactional
@Modifying(clearAutomatically = true)
@Query(value = "update T1 set value = :value where id = :id", 
nativeQuery = true)
int updateValue(Integer value, Long id);

Upvotes: 5

Related Questions