@Query in Spring Repository not update rows and not get error

192 views Asked by At

I create custom method in Spring repository

@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);

and call it in service:

log.info("started");
int i = orderRepository.updateOrderStatus(OrderStatus.CREATED, OrderStatus.PENDING);
log.info("updates: " + i);

And I see in logs:

INF 15:43:45.102 started
INF 15:43:46.035 updates: 0

In DB I have 3 rows with CREATED status. I have not err and rows not update.

@Column(name = "order_status")
@Enumerated(EnumType.STRING)
private OrderStatus orderStatus;

findAllByOrderStatus(OrderStatus.CREATED) get 3 rows
1

There are 1 answers

0
Mário Fernandes On BEST ANSWER

Your query is the other way around:

    @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);