春季交易回滚问题

发布于 2025-02-11 11:36:09 字数 1020 浏览 1 评论 0原文

在回滚保存数据时面对问题。

问题: 在我的Java方法中,有三个储蓄动作。数据保存到其他表。我想要保存失败时,所有保存的交易将是回滚。

例如,有三个表由主表和子表组成。所有表保存在方法中。 apvoucherentrydetails是子表,当它保存失败时,上面的交易应该是回滚。

我该怎么做?

提前致谢

    @Override
public ApiResponse saveVoucher(APVoucherBatch batch) {
    APVoucherBatch savedBatch = voucherBatchRepo.save(batch);  //table 1
    //==========save entry=============
    List<APVoucherEntry> entryList = batch.getEntryList();
    for(APVoucherEntry info: entryList){
        info.setVoucherBatch(savedBatch);
        info.setBatchNo(savedBatch.getBatchNo());
        APVoucherEntry savedEntry = entryRepo.save(info);   //table 2

        //==========save detail=============
        List<APVoucherEntryDetails> detailsList = info.getDetailsList();
        for(APVoucherEntryDetails dtl: detailsList){
            dtl.setBatchList(savedBatch);
            dtl.setEntryInfo(savedEntry);
            detailsRepo.save(dtl);     //table 3
        }
    }
    return new ApiResponse();
}

Facing an issue while rollback saving data.

Problem:
In my java method there are three saving actions. Data save to different table. I want when a saving fails then all saved transaction will be rollback.

Example, there are three tables consist of master table and sub table. All table save in a method. APVoucherEntryDetails is the child table, when it saves fail then upper transactions should be rollback.

How can I do it ?

Thanks in advance

    @Override
public ApiResponse saveVoucher(APVoucherBatch batch) {
    APVoucherBatch savedBatch = voucherBatchRepo.save(batch);  //table 1
    //==========save entry=============
    List<APVoucherEntry> entryList = batch.getEntryList();
    for(APVoucherEntry info: entryList){
        info.setVoucherBatch(savedBatch);
        info.setBatchNo(savedBatch.getBatchNo());
        APVoucherEntry savedEntry = entryRepo.save(info);   //table 2

        //==========save detail=============
        List<APVoucherEntryDetails> detailsList = info.getDetailsList();
        for(APVoucherEntryDetails dtl: detailsList){
            dtl.setBatchList(savedBatch);
            dtl.setEntryInfo(savedEntry);
            detailsRepo.save(dtl);     //table 3
        }
    }
    return new ApiResponse();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文