首页 > 编程语言 > 详细

Spring JPA错误——No EntityManager with actual transaction available for current thread - cannot reliably process 'remove' call

时间:2021-04-13 23:54:58      阅读:46      评论:0      收藏:0      [点我收藏+]

背景

  • 项目中使用删除+增加代替更新逻辑,在对应的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);
}

Spring JPA错误——No EntityManager with actual transaction available for current thread - cannot reliably process 'remove' call

原文:https://www.cnblogs.com/zuiyue_jing/p/14653216.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!