let\'s assume we have a Spring Data repository interface with a custom method...
@Modifying
@Transactional
@Query(\"UPDATE MyEntity SET deletedAt = CURRENT_T
Set clearAutomatically attribute on @Modifying annotation.That will clear all the non-flushed values from EntityManager.
@Modifying(clearAutomatically=true)
@Transactional
@Query("UPDATE MyEntity SET deletedAt = CURRENT_TIMESTAMP WHERE id = ?1")
MyEntity markAsSoftDeleted(long id);
To flush your changes before committing the update latest spring-data-jpa has another attribute on @ModifyingAttribute. But I think its still in 2.1.M1 release.
@Modifying(clearAutomatically=true, flushAutomatically = true)
Please check corresponding jira bug request: https://jira.spring.io/browse/DATAJPA-806
Another approach can be you can implement custom repostiory Implementation and return your updated entity after done with the query execution.
Reference : Spring data jpa custom repository implemenation