背景
- 项目中使用删除+增加代替更新逻辑,在对应的service方法上增加事务处理
@Override @Transactional public RetResult update(SysUserTable sysUserTable) { if (null != sysUserTable) { //删除关联 List<SuperSuPro> superSuPros = superSuProRepository.findByUserUid(userUid); List<String> supersuUids = new LinkedList<>(); superSuPros.forEach(superSuPro -> { String supersuUid = superSuPro.getSupersuUid(); supersuUids.add(supersuUid); }); superSuProRepository.deleteBySupersuUidIn(supersuUids); //增加关联 farmCodes.forEach(farmCode->{ SuperSuPro superSuPro = new SuperSuPro(); superSuPro.setSuperRandomUUID(); superSuPro.setUserUid(userUid); superSuPro.setFarmCode(farmCode); superSuProRepository.save(superSuPro); }); return ResultUtil.success("修改成功"); } return ResultUtil.error(-1, "修改失败,请联系管理员"); }
- 结果报错,错误信息如下
No EntityManager with actual transaction available for current thread - cannot reliably process 'remove' call
原因
- JPA接口deleteBySupersuUidIn(String uid)没有使用事务管理
@Repository public interface SuperSuProRepository extends BaseRepository<SuperSuPro> { void deleteBySupersuUidIn(List<String> supersuUids); }
解决办法
- 在deleteBySupersuUidIn(String uid)接口上使用注解@Modifying
@Repository public interface SuperSuProRepository extends BaseRepository<SuperSuPro> { @Modifying void deleteBySupersuUidIn(List<String> supersuUids); }