我在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
在DB中,我有3行CREATED
状态。我没有错误,行也没有更新。
@Column(name = "order_status")
@Enumerated(EnumType.STRING)
private OrderStatus orderStatus;
findAllByOrderStatus(OrderStatus.CREATED) get 3 rows
答案 0 :(得分:3)
您的查询是相反的:
@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);