JPA批量添加原文
出现相同标识原文
执行隔离工作时出错原文
·
·
·
·
·
·
·
·
·
·
@Transactional(rollbackFor = Exception.class)
@PostMapping(value = "addtestdata")
public RetResult addtestdata(@RequestParam("userId") Integer userId,
@RequestParam("recive_user_ids") List<String> recive_user_ids) throws Exception {
try {
//插入单条
ErpNoticeEntity erpNoticeEntity = new ErpNoticeEntity();
erpNoticeEntity.setSource("ERP");
Integer notice_id = noticeRepository.save(erpNoticeEntity).getId();
List<ErpNoticeChildEntity> list = new ArrayList<>();
for (String id : recive_user_ids) {
ErpNoticeChildEntity erpNoticeChildEntity = new ErpNoticeChildEntity();
erpNoticeChildEntity.setUserId(Integer.valueOf(id));
erpNoticeChildEntity.setNoticeId(notice_id);
erpNoticeChildEntity.setStatus(1);
list.add(erpNoticeChildEntity);
}
System.out.println(list);
noticeChildRepository.saveAllAndFlush(list);
} catch (Exception e) {
//回滚
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
throw e;
}
return new RetResult(RetCode.SUCCESS.getCode(), "操作结束");
}
出现问题:
A different object with the same identifier value was already associated wit
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)//添加这一行
public int getId() {
return id;
}