79764462

Date: 2025-09-14 17:51:16
Score: 0.5
Natty:
Report link

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 to true, 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 the readOnly flag set to false.

Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @Transactional
  • Low reputation (1):
Posted by: adityam_31