1

I am new in Spring Data JPA and will use @Modifying(clearAutomatically = true, flushAutomatically = true) annotation for an update method that updated a name field of Product entity. I have 2 questions that made me confused:

1. Do we need to use @Transactional with @Modifying annotation?

2. What does @Modifying exactly do? I read several pages e.g. Spring Data JPA @Modifying Annotation, and now too confused.

2
  • Does anybody else has never used @Modifying() ever before??? Commented Nov 12, 2021 at 12:57
  • Any reply please? Commented Nov 12, 2021 at 12:57

1 Answer 1

1

Second question first: the Modifying annotation lets you execute DML (inserts, updates, deletes) and DDL using JPA Query annotations where you put the DML or DDL where the query would normally go.

To answer the first question, i would expect to use this in a service layer annotated with @Transactional instead of putting the annotation on the Repository, because these operations seem likely to occur as part of a bigger operation with other business logic. But if that is not the case then the annotation could go on the Repository.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot, voted up. In this scene, if we use a custom query (JPQL for example) to make inserts, updates, deletes, then we will use @Modifying. Is that true?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.