zoukankan      html  css  js  c++  java
  • Spring JPA错误——No EntityManager with actual transaction available for current thread

    背景

    • 项目中使用删除+增加代替更新逻辑,在对应的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);
    }
  • 相关阅读:
    锚点
    autoLayout
    基础动画
    核心动画
    get和post的区别
    block的定义及使用
    传值-自定义构造函数传值
    字符串
    字典与可变字典
    RabbitMQ的可视化界面进行操作
  • 原文地址:https://www.cnblogs.com/zuiyue_jing/p/14653216.html
Copyright © 2011-2022 走看看