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?
CodePudding user response:
I guess this post can help you for @Modifying
at least: why do we have to use @Modifying annotation for queries in Data Jpa
CodePudding user response:
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.@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 .