简体   繁体   English

@Modifying 注解在最新的 Spring Data JPA 版本中?

[英]@Modifying annotation in the latest Spring Data JPA versions?

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 .我是 Spring Data JPA 的新手,当我搜索时,我还阅读了Spring Data JPA @Modifying Annotation以及为什么我们必须在 Data Jpa 中对查询使用 @Modifying 注释

After reading the accepted answer on SO page, I am confused.在阅读了 SO 页面上接受的答案后,我很困惑。 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? 1.我们是否还需要在 Spring Data JPA 的最后一个版本中使用 @Modifying Annotation? 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? 2.我也想知道类似的问题对@Transactional注解是否有效? Should we also need to use it for the create, update and delete methods in Spring Boot service methods?我们是否还需要将它用于 Spring Boot 服务方法中的创建、更新和删除方法? If so, could you also give a proper usage examples for an example scenario?如果是这样,您能否为示例场景提供适当的使用示例?

  1. from what i understand from the references, yes you have to use @Modifying for an Insert/create/delete ddl query.根据我从参考资料中了解到的情况,是的,您必须使用@Modifying进行插入/创建/删除 ddl 查询。 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.如果您在另一个更新/修改操作之前或之后执行更多更新/修改操作,则必须使用@Modifying(clearAutomatically=true, flushAutomatically=true) In the given SO he clearly stated whats happening if you are not using those two flags.在给定的 SO 中,他清楚地说明了如果您不使用这两个标志会发生什么。

  2. @Transactional should use for the service method/ business method. @Transactional应该用于服务方法/业务方法。 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 .即使您在不使用@transaction的情况下使用上述标志,这些更改也不会对其他微查询可见,因为它在不同的事务级别工作,并且会使您的业务逻辑失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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