Myc_CSDN
和Java狼狈为奸
2019-05-10 14:12

Spring Data Jpa 加了 @Transactional事物注解后数据异常

  • spring
  • java
  • hibernate

代码如下:

@Transactional
public void addAdminRole(String roleIdsStr, Long userId) {
    if (StringUtils.isBlank(roleIdsStr)) {
        throw new BusinessException(ResultCodeEnum.PARAMETER_ERROR);
    }
        // 重点是这里,在此处我只是查询了一个CommonAdmin这个实体,然后再没对这个实体做过任何curd操作,但执行完addAdminRole这个方法后,CommonAdmin数据却发生了变化
    CommonAdmin ca = commonAdminService.getOne(userId);
    if (ca == null || ca.getStatus() != CommonStatusEnum.ENABLE.getCode()) {
            throw new BusinessException(ResultCodeEnum.USER_DISABLE);
    }
        // 先删除原来的角色
    this.removeAdminAllRole(userId);
    String[] roleIds = roleIdsStr.split(",");
    List<AdminRole> list = new ArrayList<>(roleIds.length);
    AdminRole a;
    for (String s : roleIds) {
        a = new AdminRole();
        a.setId(IDGen.getId());
        a.setAdminUserId(userId);
        a.setRoleId(Long.valueOf(s));
        a.setCreateTime(System.currentTimeMillis());
        a.setStatus(CommonStatusEnum.ENABLE.getCode());
        a.setUpdateTime(System.currentTimeMillis());
        list.add(a);
    }
    this.adminRoleDao.saveAll(list);
}

其中getOne方法如下:

public CommonAdmin getOne(Long id) {
        CommonAdmin u = commonAdminDao.findById(id).get();
        u.setCertification("");
        return u;
}
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答