1

I am new in Spring data JPA and when I am searching I also read Spring Data JPA @Modifying Annotation and why do we have to use @Modifying annotation for queries in Data Jpa.

After reading the accepted answer on SO page, I am confused. Now, could you pls clarify me about the following issues?

1. Should we still need to use @Modifying Annotation in the last version(s) of Spring Data JPA? If so, could you explain how should I use properly (any annotation for proper usage)?

2. I am also wondering if the similar issue is valid for @Transactional annotation? Should we also need to use it for the create, update and delete methods in Spring Boot service methods? If so, could you also give a proper usage examples for an example scenario?

1 Answers1

0
  1. from what i understand from the references, yes you have to use @Modifying for an Insert/create/delete ddl query. And you have to use @Modifying(clearAutomatically=true, flushAutomatically=true) if you are doing more update/modifying operations before or after another update/modifying operations. In the given SO he clearly stated whats happening if you are not using those two flags.

  2. @Transactional should use for the service method/ business method. if you execute set of selections, updates, deletion in one business logic, those will be grouped and one persistence context will be used for them. so your micro query changes are visible to other micro queries with in the transaction(there can be many micro queries in your business logic code). Even if you use those above flags without using @transaction those changes wont visible to other micro queries as its work in different transaction level and which will fail your business logic .

namila007
  • 1,044
  • 9
  • 26
  • Thanks a lot for your nice explanations, **voted up**. In this scene, I inferred the following points from your explanations. Could you please confirm or clarify me for each of these points? –  Jun 30 '22 at 15:44
  • **1.** We use `@Modifying` annotation for Create / Update / Delete service methods only when we use Insert / Update / Delete using `@Query()` annotation in the Spring Data JPA Repository. Is that all true? –  Jun 30 '22 at 16:05
  • **2.** Ok, the safest way to use @Modifying is `@Modifying(clearAutomatically=true, flushAutomatically=true)`. But what about there is only just a single update operation? In this case should we still use like that or just `@Modifying()` annotation? –  Jun 30 '22 at 16:05
  • **3.** Should we use `@Modifying` and `@Transactional` annotations just above the Spring Data JPA methods where `@Query()` annotation is used? Or can we also use both of them or one of them above the service methods? –  Jun 30 '22 at 16:05
  • **4.** I think we use `@Transactional` annotation on the Create / Update / Delete methods in the service. Is that right? And should we use it for all of the Create / Update / Delete methods or only the methods where multiple operations are done e.g. affecting multiple table? –  Jun 30 '22 at 16:05
  • Amigo? Any reply please? –  Jul 01 '22 at 06:49
  • 1. `@Query` is used only for selections(Read). this cant modify any data. so you always has to use `@Modifying` for your modifying queries. – namila007 Jul 01 '22 at 14:17
  • 2. what if your web app handle multiple connections in multiple threads? i think using those args are the safest. – namila007 Jul 01 '22 at 14:19
  • 3. `@Modifying()` is usually used for queries, while `transaction` for service layer code, so if your service fn handle different cruds ops on different tables and all those are interconected you need to wrap them in a single transaction,if one failed, all will rollback. please read this SO https://stackoverflow.com/questions/48314475/do-we-need-both-transactional-and-modifying-annotation-in-spring you can get an idea for both 3 and 4 Qs – namila007 Jul 01 '22 at 14:25
  • @Jonathan sorry for late replies :) – namila007 Jul 01 '22 at 14:26
  • No problem amigo, thanks a lot for your valuable helps ;) regarding to your answers: –  Jul 01 '22 at 16:10
  • **1.** Then for `SELECT` queries we use `@Query` annotation and for `INSERT`, `UPDATE` or `DELETE` queries we use `@Modifying()` above the `@Query` annotation. Is that true? –  Jul 01 '22 at 16:12
  • **2.** Do you mean that we should always use as `@Modifying(clearAutomatically=true, flushAutomatically=true)` even if there is a single operation/entity or multiple? If so, I hope these 2 parameters would not give extra workload for single operations. Any clarification pls? –  Jul 01 '22 at 16:14
  • **3.** I read that question you suggested, but the answer is not explained well as you explained :) So, as far as I see, we use `@Modifying()` above the `@Query` annotation in the repository and `@Transactional` above the service methods. Is that right? –  Jul 01 '22 at 16:21
  • **4.** You said we use `@Transactional` annotation only when "*our service method handle different cruds ops on different tables and all those are interconected*. **In this scene**, if our service method just makes a read operation or makes a single create / update / delete and does not need transaction, then do we still need to use `@Transactional` annotation? –  Jul 01 '22 at 16:25
  • you are repeating again. please read again :) 1. yes , 2. yes i think so, 3.yes, 4.no need, but its a good practice – namila007 Jul 03 '22 at 14:44
  • if i gave ur answers, please approve as the answer thank you – namila007 Jul 03 '22 at 14:44
  • By the way, if the question is an acceptable one please do not forget to vote up. Thanks a lot. –  Jul 05 '22 at 11:53