簡體   English   中英

Spring @Transactional方法問題

[英]Spring @Transactional method issue

因此,我有一個控制器[POST]方法,該方法基本上應該用於創建transaction

邏輯很簡單,在創建transaction時應涉及兩個帳戶 (這是方案),我向transaction傳遞了3個字段- amountemitterIdreceptorId 正在創建交易后,senderBalance應減少 transaction.amount和receiverBalance應增加 transaction.amount ,但由於某種原因沒有任何變化

這是代碼:

@Autowired
    private AccountRepository accountRepository;

    @Autowired
    private TransactionRepository transactionRepository;

@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();
        Integer emitterBalance = accountRepository.findById(result.getSenderAccountId()).get().getBalance();
        Integer receptorBalance = accountRepository.findById(result.getReceiverAccountId()).get().getBalance();
        Integer amount = transactionRepository.findById(result.getId()).get().getAmount();
        Integer emitterFinalBalance = emitterBalance - amount;
        Integer receptorFinalBalance = receptorBalance + amount;
        accountRepository.findById(result.getSenderAccountId()).get().setBalance(emitterFinalBalance);
        accountRepository.findById(result.getReceiverAccountId()).get().setBalance(receptorFinalBalance);
        transactionRepository.save(result);
        transactionRepository.save(transaction);
        return ResponseEntity.created(location).build();
    }

實體交易

public class Transaction {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotNull
    private Integer amount;
    private Instant created;
    private Long senderAccountId;
    private Long receiverAccountId;

    @JsonIgnore
    @ManyToOne
    @JoinColumn(name = "emitterId")
    private Account emitterId;

    @JsonIgnore
    @ManyToOne
    @JoinColumn(name = "receptorId")
    private Account receptorId;

    public Transaction(Integer amount, Account emitterId, Account receptorId){
        this.created = Instant.now();
        this.amount = amount;
        this.emitterId = emitterId;
        this.receptorId = receptorId;
        senderAccountId = this.emitterId.getId();
        receiverAccountId = this.receptorId.getId();
    }

}

帳戶

public class Account {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotNull
    private String holder;

    @NotNull
    private Integer balance;

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

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

    public Account(String holder, Integer balance){
        this.holder = holder;
        this.balance = balance;
    }

    public Account(Long id){
        this.id = id;
    }

}

您能幫我解決這個問題嗎:)?

謝謝您的參與!

似乎您沒有保留要更新其余額的帳戶對象。 您是否具有級聯從事務到單個帳戶的實體映射,以便一旦保存事務,相應的帳戶對象也將保留。

請嘗試以下操作:

    accountRepository.findById(result.getSenderAccountId()).get().setBalance(emitterFinalBalance);

accountRepository.findById(result.getReceiverAccountId()).get().setBalance(receptorFinalBalance);

用。。。來代替

Account sender = accountRepository.findById(result.getSenderAccountId()).get();
sender.setBalance(emitterFinalBalance);
accountRepository.save(sender);

Account receiver = accountRepository.findById(result.getReceiverAccountId()).get();
receiver.setBalance(receptorFinalBalance);
accountRepository.save(receiver);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM