控制器方法持久性问题 [英] Controller method persistence issue

查看:64
本文介绍了控制器方法持久性问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情况:两个实体-TransactionAccount 交易仅涉及两个帐户.

I have following scenario: two Entities - Transaction and Account Transaction involves only two accounts.

创建交易后,来自accountSender的金额减少,而accountReceiver的金额增加.

After creation of transaction, amount of money from accountSender decrease while money for accountReceiver increase.

Account实体包含两个列表-transactionsMadeListtransactionsReceivedList,因此总体上看起来像这样:

Account entity holds two lists - transactionsMadeList and transactionsReceivedList so overall it looks like that:

[
    {
        "id": 1,
        "holder": "Pavel S",
        "balance": -950,
        "transactionsMade": [
            {
                "id": 1,
                "amount": 50,
                "created": "2019-09-01T13:37:24.565579Z",
                "senderAccountId": 1,
                "receiverAccountId": 2
            },
            {
                "id": 3,
                "amount": 35,
                "created": "2019-09-01T13:37:24.614450Z",
                "senderAccountId": 1,
                "receiverAccountId": 2
            }
        ],
        "transactionsReceived": [
            {
                "id": 2,
                "amount": 50,
                "created": "2019-09-01T13:37:24.609462Z",
                "senderAccountId": 2,
                "receiverAccountId": 1
            }
        ]
    },
    {
        "id": 2,
        "holder": "Gustaf S",
        "balance": 1070,
        "transactionsMade": [
            {
                "id": 2,
                "amount": 50,
                "created": "2019-09-01T13:37:24.609462Z",
                "senderAccountId": 2,
                "receiverAccountId": 1
            }
        ],
        "transactionsReceived": [
            {
                "id": 1,
                "amount": 50,
                "created": "2019-09-01T13:37:24.565579Z",
                "senderAccountId": 1,
                "receiverAccountId": 2
            },
            {
                "id": 3,
                "amount": 35,
                "created": "2019-09-01T13:37:24.614450Z",
                "senderAccountId": 1,
                "receiverAccountId": 2
            }
        ]
    }
]

我实现了创建交易的Controller方法[POST]. 下面的代码:

I implemented Controller method [POST] which creates a Transaction. Code below:

@Transactional
    @PostMapping("/transactions")
    public ResponseEntity<Transaction> createTransaction(@Valid @RequestBody Transaction transaction) {
        final Transaction result = transactionRepository.save(transaction);
        final URI location = ServletUriComponentsBuilder.fromCurrentRequest().
                path("/{id}")
                .buildAndExpand(result.getId()).toUri();
        Account sender = accountRepository.findById(result.getSenderAccountId()).get();
        Account receiver = accountRepository.findById(result.getReceiverAccountId()).get();
        Transaction tr = transactionRepository.findById(result.getId()).get();
        Integer emitterBalance = accountRepository.findById(result.getSenderAccountId()).get().getBalance();
        Integer receptorBalance = accountRepository.findById(result.getReceiverAccountId()).get().getBalance();
        Integer amount = tr.getAmount();
        Integer emitterFinalBalance = emitterBalance - amount;
        Integer receptorFinalBalance = receptorBalance + amount;


        sender.setBalance(emitterFinalBalance);
        accountRepository.save(sender);

        receiver.setBalance(receptorFinalBalance);
        accountRepository.save(receiver);

        transactionRepository.save(result);
        transactionRepository.save(tr);
        return ResponseEntity.created(location).build();
    }

我的问题是:当我从CommandLineRunner间接静态变量将数据持久保存到数据库时-事务对象被保存,然后显示在transactionsMadeList/transactionsReceivedList中,也可以通过以下方式找到[GET] ~/transactions方法 但是,当我尝试通过[POST] ~/transactions方法创建Transaction时,正在创建Transaction,但没有将其添加到transactionsMade/transactionsReceived lists

My question is: When I am persisting data to the database from a CommandLineRunner indirect static variables - transaction object is getting saved and then getting displayed in transactionsMadeList/transactionsReceivedList and also could be found by [GET] ~/transactions method But when I am trying to create Transaction via [POST] ~/transactions method, Transaction is getting created but is not getting added to the transactionsMade/transactionsReceived lists

感谢您的帮助,因为我现在有点:/ 看来问题出在Controller方法的持久性部分中 如果您需要实体类,只需告诉我我也将它们发布

Would appreciate any help as I am kinda stack now with that :/ It seems that problem is in the persisting part in the Controller method In case you will need Entity classes just tell me Ill post them as well

推荐答案

您应将交易添加到帐户中. (已移除mappedBy) 尝试以下操作:

You should add the transactions into accounts. (Removed mappedBy) Try following:

帐户类别更改

@OneToMany(fetch = FetchType.LAZY,cascade = CascadeType.ALL)
private List<Transaction> transactionsMade = new ArrayList<>();

@OneToMany(fetch = FetchType.LAZY,cascade = CascadeType.ALL)
private List<Transaction> transactionsReceived = new ArrayList<>();

控制器:

@Transactional
@PostMapping("/transactions")
public ResponseEntity<Transaction> createTransaction(@Valid @RequestBody Transaction transaction) {
    final Transaction storedTransaction = transactionRepository.save(transaction);
    final URI location = ServletUriComponentsBuilder.fromCurrentRequest().
            path("/{id}")
            .buildAndExpand(storedTransaction.getId()).toUri();
    Account sender = accountRepository.findById(storedTransaction.getSenderAccountId()).get();
    Account receiver = accountRepository.findById(storedTransaction.getReceiverAccountId()).get();
    Integer emitterBalance = accountRepository.findById(storedTransaction.getSenderAccountId()).get().getBalance();
    Integer receptorBalance = accountRepository.findById(storedTransaction.getReceiverAccountId()).get().getBalance();
    Integer amount = storedTransaction.getAmount();
    Integer emitterFinalBalance = emitterBalance - amount;
    Integer receptorFinalBalance = receptorBalance + amount;

    sender.setBalance(emitterFinalBalance);
    sender.getTransactionsMade().add(storedTransaction);
    accountRepository.save(sender);

    receiver.setBalance(receptorFinalBalance);
    receiver.getTransactionsReceived().add(storedTransaction);
    accountRepository.save(receiver);

    return ResponseEntity.created(location).build();
}

这篇关于控制器方法持久性问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆