有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在异常发生后放弃hibernate/JPA中的更改?

异常后如何放弃更改?我想保存recordB,即使recordA无法保存

//The transaction comes from else where, cannot modify it
@Transactional
void updateRecord{
    Record recordA = repository.findById(ida);
    Record recordB = repository.findById(idb);
    recordA.setXXX();
    recordB.setXXX();
    try {
        repository.saveAndFlush(recordA);
    } catch (Exception exp) {
        if (exp instanceof DataIntegrityViolationException) {
            entityManager.detach(recordA);
        } else {
            throw new RuntimeException(exp);
        }
    }
    repository.saveAndFlush(recordB);//got exception cause recordA still engaged
}

共 (0) 个答案