简体   繁体   中英

Spring Data JPA @Modifying annotation usage with @Transactional

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 eg Spring Data JPA @Modifying Annotation , and now too confused.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM