当我将FindbyId与JPA一起使用时,我可以将读书设置为false

发布于 2025-01-22 00:16:34 字数 856 浏览 3 评论 0原文

在DB复制之前设置数据库复制时,我在修改数据时遇到了问题

,我将获得要使用repository.findbyid()进行修改的数据,然后我修改了数据。 但是我意识到repository.findbyid()是设置@transactional(readonly = true) in simple> simplejparepository.java ,以便我无法修改即使我在服务或repository.save()中使用@transactional,

是否还有其他方法强制findbyid()通过写连接连接,除非通过制作FindbyId存储库中的自定义方法?

+++) 我解决了我的问题!我想使用肮脏的检查进行修改数据,我意识到我关于EntityManagerFactory的设置是错误的,我在Spring.io中使用DOC修复了它( https://docs.spring.io/spring-data/jpa/jpa/jpa/docs/current-snaps/current-snapshot/current-snapshot/reference/reference/Reference/Reference/hhtml/#一下参考)我在许多其他开发人员发布的情况下尝试了很多次,但他们对我不起作用,但确实如此。谢谢你给我答案

I have a problem with modifying data while setting database replication

Before DB replication, I get data that I want to modify using repository.findById() and then I modified the data.
But I realized that repository.findById() is set @Transactional(readOnly = true) in SimpleJpaRepository.java so that I can't modify the data even if I use @Transactional in my Service or repository.save()

Is there any other way to force findById() to connect by a write connection except by making custom method in the repository for findById?

+++)
I solved my problem! I wanted to use dirty checking for modifying datas and I realized that my setting about EntityManagerFactory was something wrong and I fixed it with a doc in spring.io (https://docs.spring.io/spring-data/jpa/docs/current-SNAPSHOT/reference/html/#reference) I tried many times with many other developers posting but they didn't work for me, but it did. Thank you for giving me answers ????

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光沙漏 2025-01-29 00:16:34

请参阅此,
-data/jpa/docs/current/reference/html/#transactions

第5.7.1节。交易查询方法是更具体的
它说@modifying注释可用于覆盖事务配置

通常,您将使用“读取标志”设置为true,因为大多数查询方法都是读取的标志。与该DeleteInActiveUser()相反,使用@Modifying注释并覆盖事务配置。因此,该方法将使用“读取标志”设置为false执行。

Refer this,
https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#transactions

Section 5.7.1. Transactional query methods to be more specific
It says that @Modifying annotation can be used to override transaction configuration

Typically you will use the readOnly flag set to true as most of the query methods will be reading ones. In contrast to that deleteInactiveUsers() makes use of the @Modifying annotation and overrides the transaction configuration. Thus the method will be executed with readOnly flag set to false.

葵雨 2025-01-29 00:16:34

您无需更改该标志!

  1. 找到使用@Modifying的数据
  2. 编辑数据
  3. 调用JPA Repository.Save(NewData)方法,以将编辑的数据保存在DB

,即

@transactional
@modifying
@query(value =“更新用户设置点=点 +?1
id =?2“,nativequery = true)
int增加ePoints(int点,长ID);

You don't need to change that flag!

  1. Find the data
  2. Edit data
  3. Call JPA repository.save(newData) method with @Modifying to save the edited data in the DB

I.E.
@Transactional
@Modifying
@Query(value = "UPDATE user SET points = points + ?1
WHERE id = ?2", nativeQuery = true)
int increasePoints(int points, Long id);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文