Фреймворк ManyToOne MyBatis - PullRequest
       33

Фреймворк ManyToOne MyBatis

0 голосов
/ 03 августа 2020
• 1000 чтобы получить все балансы, связанные со счетом, и балансы по-прежнему возвращаются как нулевые. Был бы признателен за любую помощь здесь
   @Select("Select accountId,customerId from account where accountId=#{accountId}")
    @Results(value = { @Result(property = "accountId", column = "accountId"), @Result(property = "customerId", column = "customerId"),
            @Result(property = "balances", javaType = List.class, column = "accountId", many = @Many(select = "getBalances"))

    })
    Optional<Account> getAccount(@Param("id") Long id);

    @Select("select currency,balanceId,accountId,amount from balance where accountId=#{accountId}")
    List<Balance> getBalances(Long accountId);


public class Account {

@Id
private Long accountId;

@Column("country")
private String country;

@Column("customerId")
private Long customerId;

List<Balance> balances;

Здесь немного сложно, я новичок в своем батисе, поэтому пытаюсь найти способ обойти его, чтобы делать именно то, что мне нужно, и он работает, кроме случаев, когда я пытаюсь получить учетную запись который должен отображать все, включая балансы

Я в основном сначала проверяю, существует ли учетная запись, если я не создаю учетную запись, тогда я получаю список валют из запроса и строю Список балансов, включая начальную сумму, а также заполняю это сгенерированный идентификатор учетной записи.

 public Account create(AccountCreateRequest request) {
    Optional<Long> customerId = accountMapper.checkIfCustomerExist(request.getCustomerId());

    if (customerId.isEmpty()) {
        Account account = Account.builder().customerId(request.getCustomerId()).country(request.getCountry()).build();
        accountMapper.saveAccount(account);
        List<Balance> balances = request.getCurrencies().stream().map(currency -> Balance.builder()
                .currency(currency)
                .amount(BigDecimal.ZERO)
                .build()).map(b -> b.setAccountId(account.getAccountId())).collect(Collectors.toList());
        balances.forEach(accountMapper::saveBalance);
        account.setBalances(balances);
        return account;
    } else {
        throw new RuntimeException("Account already exist");
    }
}
...