Whenever implementing data modifying logic using Query
annotation (insert
, update
or delete
) in JpaRepository
, both @Transactional
(not necessarily on the repository method) and @Modifying
have to be used.
My understanding is as follows. All crud operations provided by JpaRepository
are transactional unless we overwrite them. When implementing a method in a repository, we need to make sure it's still transactional. It can be achieved simply by annotating with @Transactional
. The default value of readOnly
is false
, hence spring "knows" it's a modifying query.
The question is: why do we need @Modifying
annotation then (together with @Transactional
)? Maybe I'm missing something?
I'm aware of the discussions like this or that, but I'm missing an explicit explanation for why @Modifying
has to be used if @Transactional
carries all the information needed.