+-

我有一种情况,需要首先根据条件更新一些数据库记录,然后再从数据库中读取这些记录.我正在使用CrudReposirtory,并且在我的控制器中有一个服务,该服务使用@Query调用存储库方法来更新记录,并在下一行尝试读取相同的记录,但是除非我不在意,否则记录不会更新控制器方法.
最佳答案
如果执行UPDATE语句,则应将@Modifying与@Query结合使用.
从文档:
@Modifying
@Query("update User u set u.firstname = ?1 where u.lastname = ?2")
int setFixedFirstnameFor(String firstname, String lastname);
This will trigger the query annotated to the method as updating query instead of a selecting one. As the EntityManager might contain outdated entities after the execution of the modifying query, we automatically clear it (see JavaDoc of EntityManager.clear() for details). This will effectively drop all non-flushed changes still pending in the EntityManager. If you don’t wish the EntityManager to be cleared automatically you can set @Modifying annotation’s clearAutomatically attribute to false;
点击查看更多相关文章
转载注明原文:Spring Boot JPA-CrudRepository更新,然后阅读 - 乐贴网