Executing an update/delete query; nested exception is javax.persistence.TransactionRequiredException

问题:在调用JPA进行更新操作时报错如下:

Executing an update/delete query; nested exception is javax.persistence.TransactionRequiredException:

代码:

Executing an update/delete query; nested exception is javax.persistence.TransactionRequiredException_第1张图片

原因:其实提示已经很明确了,没有使用事物

解决方案:方法上增加@Transactional

 

@Transactional

@Modifying(clearAutomatically = true)

@Query("update ToOrderVO toOrderVO set toOrderVO.orderStatusId = ?3 where toOrderVO.orderCode = ?1 and toOrderVO.orderStatusId=?2")

public int updateByOrderCodeAndOrderStatusId(@Param(value = "order_code") String helperCode,@Param(value = "order_status_id") int orderStatusIdFrom ,@Param(value = "order_status_id") int orderStatusIdTo);

 

备注:

1.更新info表下指定id的status字段, 这里使用了原生的sql语句。

2.@Transactional 注解用于提交事务,若没有带上这句,会报事务异常提示。

3.@Modifying(clearAutomatically = true) 自动清除实体里保存的数据。

你可能感兴趣的:(JPA)