【问题标题】:Different event on different projects不同项目的不同活动
【发布时间】:2021-04-02 00:43:42
【问题描述】:

我在 2 个不同的项目上部署了同一个 jar。该项目有 2 个实体(帐户和买方),该帐户有一个买方列表,一个买方有一个帐户。当我更改买家的帐户时,我需要将买家从一个 List 中删除并添加到另一个买家 List 中,对吗?以下代码显示了这一点。 但问题是,在项目 A 上,当我运行买方保留在两个列表中的代码时,当我运行项目 B 时,买方从旧的帐户列表中删除并添加到新的帐户列表中。 我的数据存储有问题吗?

// Account class resumed
public class Account implements Serializable {
    ...
    @Reference
    private List<Buyer> buyers = new ArrayList<>();
    ...
}
//  Account class resumed
public class Buyer implements Serializable {
    ...
    @Reference
    private Account account;
    ...
}
// The transaction
@Transactional
public void update(Long id, Buyer updatedBuyer) {
    Buyer buyer = find(id);
    Buyer found = repository.findByEmailAndAccount(updatedBuyer.getEmail(), updatedBuyer.getAccount()).orElse(null);
    if (found != null && !found.getId().equals(id)) {
        throw new DataIntegrityException("Comprador já cadastrado");
    }
    accountService.deleteBuyers(buyer.getAccount(), buyer);
    buyer.setEmail(updatedBuyer.getEmail());
    buyer.setTelephone(updatedBuyer.getTelephone());
    buyer.setCreationDate(updatedBuyer.getCreationDate());
    buyer.setExpirationDate(updatedBuyer.getExpirationDate());
    buyer.setAccount(updatedBuyer.getAccount());
    buyer.getAccount().addBuyer(buyer);
    repository.save(buyer);
}
// The deleteBuyers used above
public void deleteBuyers(Account account, Buyer... buyers) {
    for (Buyer b : buyers) {
        account.getBuyers().remove(b);
    }
    repository.save(account);
}
// The updatedBuyer came from here
public Buyer fromDTO(PatchBuyerDTO patchBuyerDTO) {
    return new Buyer(patchBuyerDTO.getEmail(), patchBuyerDTO.getTelephone(), false, patchBuyerDTO.getCreationDate(), patchBuyerDTO.getExpirationDate(), accountService.find(patchBuyerDTO.getAccountDTO().getId()));
}

【问题讨论】:

    标签: spring-boot google-app-engine google-cloud-platform google-cloud-datastore


    【解决方案1】:

    问题在于创建,我没有保存帐户,只有买家

    // before
    return repository.save(buyer)
    // after
    repository.saveAll(Arrays.asList(buyer));
    return buyer;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-08
      • 1970-01-01
      • 2013-11-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多