当我将 findById 与 JPA 一起使用时,我可以设置 readOnly false 吗?

Can I set readOnly false when I use findById with JPA

我在设置数据库复制时修改数据时遇到问题

在数据库复制之前,我使用 repository.findById() 获取要修改的数据,然后修改了数据。 但是我意识到 repository.findById()SimpleJpaRepository.java 中设置了 @Transactional(readOnly = true) 这样即使我在我的服务中使用 @Transactional 或 repository.save()[=17= 我也无法修改数据]

除了在存储库中为 findById 创建自定义方法外,还有其他方法可以强制 findById() 通过写连接进行连接吗?

+++) 我解决了我的问题!我想使用脏检查来修改数据,我意识到我关于 EntityManagerFactory 的设置有问题,我用 spring.io (https://docs.spring.io/spring-data/jpa/docs/current-SNAPSHOT/reference/html/#reference) 中的文档修复了它我尝试了很多次与许多其他开发人员发帖但是他们没有为我工作,但确实如此。谢谢你给我答案

参考这个,
https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#transactions
第 5.7.1 节。交易查询方式更具体
它说@Modifying注解可以用来覆盖事务配置

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.

您不需要更改该标志!

  1. 查找数据
  2. 编辑数据
  3. 使用@Modifying调用JPA repository.save(newData)方法将编辑后的数据保存到DB

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