org.springframework.orm.hibernate4.HibernateOptimisticLockingFailureException: Batch update returne

本文详细探讨了Spring框架中事务配置不当导致的问题及其解决方案。特别是针对以get开头的方法被误配置为事务性的常见错误,提供了具体的修改建议。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

错误记录:

问题描述:


原因:

spring事务配置的问题


在spring配置事务时(或者用注解配置事务时)若配置了以上事务 则表示以get开头的方法都启用了事务


事务的传播属性

1、PROPAGATION_REQUIRED:如果存在一个事务,则支持当前事务。如果没有事务则开启一个新的事务。

2、PROPAGATION_SUPPORTS:如果存在一个事务,支持当前事务。如果没有事务,则非事务的执行。

3、PROPAGATION_MANDATORY:如果已经存在一个事务,支持当前事务。如果没有一个活动的事务,则抛出异常。

4、PROPAGATION_REQUIRES_NEW:总是开启一个新的事务。如果一个事务存在,则将这个存在的事务挂起。

5、PROPAGATION_NOT_SUPPORTED:总是非事务地执行,并挂起任何存在的事务。

6、PROPAGATION_NEVER:总是非事务地执行,如果存在一个活动事务,则抛出异常。

7、 PROPAGATION_NESTED:如果一个活动的事务存在,则运行在一个嵌套的事务中,如果没有活动事务,则按TransactionDefinition.PROPAGATION_REQUIRED属性执行


在添加事务的页面的方法如下所示

查询方法名写的getXXX  ,如果在controller中调用多次这样的查询方法,就会出现如上问题


解决方法:

删除事务中的配置的代码,或者将命名规范

CREATE TABLE plan_detail ( plan_id BIGINT NOT NULL, dt DATE NOT NULL, planned_qty INT DEFAULT 0, actual_qty INT DEFAULT 0, PRIMARY KEY (plan_id, dt) );这是我的数据库表,以下为我的后端接口package com.example.staffmanagement.controller; import com.example.staffmanagement.entity.PlanDetail; import com.example.staffmanagement.service.PlanDetailService; import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController @RequestMapping("/api/production_plans") @RequiredArgsConstructor public class PlanDetailController { private final PlanDetailService planDetailService; @GetMapping("/{id}/details") public List<PlanDetail> getDetails(@PathVariable Long id) { return planDetailService.listByPlanId(id); } @PostMapping("/{id}/details") public void saveDetails(@PathVariable Long id, @RequestBody List<PlanDetail> list) { planDetailService.saveBatchByPlanId(id, list); } } package com.example.staffmanagement.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.example.staffmanagement.entity.PlanDetail; import org.apache.ibatis.annotations.Mapper; @Mapper public interface PlanDetailMapper extends BaseMapper<PlanDetail> { }package com.example.staffmanagement.service.impl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.example.staffmanagement.entity.PlanDetail; import com.example.staffmanagement.mapper.PlanDetailMapper; import com.example.staffmanagement.service.PlanDetailService; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; @Service @RequiredArgsConstructor public class PlanDetailServiceImpl extends ServiceImpl<PlanDetailMapper, PlanDetail> implements PlanDetailService { @Override public List<PlanDetail> listByPlanId(Long planId) { return lambdaQuery() .eq(PlanDetail::getPlanId, planId) .list(); } @Override @Transactional public void saveBatchByPlanId(Long planId, List<PlanDetail> list) { // 先删除旧数据 lambdaUpdate() .eq(PlanDetail::getPlanId, planId) .remove(); // 再批量插入 list.forEach(d -> d.setPlanId(planId)); saveBatch(list); } }
最新发布
07-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值