我在Spring存储库中创建自定义方法
@Transactional
@Modifying(clearAutomatically = true)
@Query("UPDATE Order co SET co.orderStatus =:status WHERE co.orderStatus =:newStatus")
int updateOrderStatus(@Param("status") OrderStatus status, @Param("newStatus") OrderStatus newStatus);
并在服务中调用它:
log.info("started");
int i = orderRepository.updateOrderStatus(OrderStatus.CREATED, OrderStatus.PENDING);
log.info("updates: " + i);
我在日志中看到:
INF 15:43:45.102开始
INF 15:43:46.035更新:0
在数据库中,我有3行具有
CREATED
状态。我没有犯错,行也没有更新。@Column(name = "order_status")
@Enumerated(EnumType.STRING)
private OrderStatus orderStatus;
findAllByOrderStatus(OrderStatus.CREATED) get 3 rows
最佳答案
您的查询是另一种方式:
@Transactional
@Modifying(clearAutomatically = true)
@Query("UPDATE Order co SET co.orderStatus =:newStatus WHERE co.orderStatus =:status")
int updateOrderStatus(@Param("status") OrderStatus status, @Param("newStatus") OrderStatus newStatus);