gpt4 book ai didi

java - Controller 方法持久化问题

转载 作者:行者123 更新时间:2023-12-02 09:33:34 24 4
gpt4 key购买 nike

我有以下场景:两个实体 - TransactionAccount交易仅涉及两个账户。

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

Account实体拥有两个列表 - transactionsMadeListtransactionsReceivedList总的来说,它看起来像这样:

[
{
"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]这会创建一个交易。代码如下:

@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 找到方法但是当我尝试创建Transaction时通过[POST] ~/transactions方法,Transaction正在创建,但未添加到 transactionsMade/transactionsReceived lists

非常感谢任何帮助,因为我现在有点堆栈了:/看来问题出在 Controller 方法的持久化部分如果您需要实体类,请告诉我,我也会发布它们

最佳答案

您应该将交易添加到帐户中。(删除了映射者)尝试以下操作:

帐户类别更改

@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<>();

Controller :

@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();
}

关于java - Controller 方法持久化问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57746512/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com