The default methods in the CrudRepository
interface will have @Transactional
annotation on them. But since you have added a custom method - void deleteByEmail(String email);
you will have to give @Transactional
explicitly either at Controller, Service or Repository level.
Ref: https://docs.spring.io/spring-data/jpa/reference/jpa/transactions.html states that -
Transactional query methods
Declared query methods (including default methods) do not get any transaction configuration applied by default. To run those methods transactionally, use
@Transactional
at the repository interface you define, as shown in the following example:
Example 3. Using @Transactional at query methods
@Transactional(readOnly = true)
interface UserRepository extends JpaRepository<User, Long> {
List<User> findByLastname(String lastname);
@Modifying
@Transactional
@Query("delete from User u where u.active = false")
void deleteInactiveUsers();
}
Typically, you want the
readOnly
flag to be set totrue
, as most of the query methods only read data. In contrast to that,deleteInactiveUsers()
makes use of the@Modifying
annotation and overrides the transaction configuration. Thus, the method runs with thereadOnly
flag set tofalse
.