Spring Data Jpa錯誤——No EntityManager with actual transaction available for current thread - cannot reliably process 'remove' call


背景

  • 項目中使用刪除+增加代替更新邏輯,在對應的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);
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM