• 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);
    }
  • 相关阅读:
    mapcontrol的鼠标样式
    SQL SERVER数据类型与C#数据类型对照表
    正则表达式基元
    WPF中UserControl和DataTemplate
    [转]SQL三种获取自增长的ID方法
    [转]SQL Server 存储过程 一些常用用法(事物、异常捕捉、循环)
    Learn JavaScript(面向对象的程序设计01)
    CLR:基元类型、引用类型和值类型
    grid layout 相关代码
    css关于position的几个值
  • 原文地址:https://www.cnblogs.com/zuiyue_jing/p/14653216.html
Copyright © 2020-2023  润新知