JPA:许多与许多关系无法正常工作

发布于 2025-01-21 23:44:24 字数 2507 浏览 0 评论 0 原文

我有两个以下实体,指令包含许多银行账户,而银行账户属于一个或多个指派。

@Entity
public class AssignIbanEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    private String assignmentIbanId;

    @ManyToMany(mappedBy = "assignIbans", cascade = CascadeType.ALL)
    private Set<BankAccountEntity> bankData;

    public void addBankData(BankAccountEntity bankData) {
        if (this.bankData == null) {
            this.bankData = new HashSet<>();
        }
        this.bankData.add(bankData);
        if (CollectionUtils.isEmpty(bankData.getAssignIbans())) {
            bankData.assignIbans(new HashSet<>());
        }
        bankData.getAssignIbans().add(this);
    }
    
}

@Entity(name = "bank_datay")
public class BankAccountEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    private String bankDataId;

    @JsonIgnore
    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "bank_account_assignment_iban", joinColumns = {
            @JoinColumn(name = "fk_bank_account") }, inverseJoinColumns = { @JoinColumn(name = "fk_assignment_iban") })
    private Set<AssignIbanEntity> assignIbans;
    
}

在我的数据库中,我有以下数据:

bank_data:

”在此处输入图像说明“

bank_account_assignment_iban:

带有以下代码我获得ID 8AA2AFBF F079-44A3-9940 -84DD01A97F7F ,我将其添加到具有ID 87F6F8AC-FD51-493D-860B-F6975FAAEA472 的存在的赋值班

AssignIbanEntity assignIban = assignmentIbanRepository.findById("87f6f8ac-fd51-493d-860b-f6975faea472");
bankDataEntity bankData = bankDataRepository.findById("8aa2afbf-f079-44a3-9940-84dd01a97f7f");
if (Objects.nonNull(assignIban) && Objects.nonNull(bankData)) {
    assignIban.addBankData(bankData);
    assignmentIbanRepository.save(assignIban);
}

问题上,问题是我无法正确的结果:

预期结果:

”在此处输入图像说明”

I have the two below entities, the AssignIban contains many BankAccounts, and a bankAccount belongs to one or many AssignIban.

@Entity
public class AssignIbanEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    private String assignmentIbanId;

    @ManyToMany(mappedBy = "assignIbans", cascade = CascadeType.ALL)
    private Set<BankAccountEntity> bankData;

    public void addBankData(BankAccountEntity bankData) {
        if (this.bankData == null) {
            this.bankData = new HashSet<>();
        }
        this.bankData.add(bankData);
        if (CollectionUtils.isEmpty(bankData.getAssignIbans())) {
            bankData.assignIbans(new HashSet<>());
        }
        bankData.getAssignIbans().add(this);
    }
    
}

@Entity(name = "bank_datay")
public class BankAccountEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    private String bankDataId;

    @JsonIgnore
    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "bank_account_assignment_iban", joinColumns = {
            @JoinColumn(name = "fk_bank_account") }, inverseJoinColumns = { @JoinColumn(name = "fk_assignment_iban") })
    private Set<AssignIbanEntity> assignIbans;
    
}

In my database i have the below data:

bank_data:

enter image description here

bank_account_assignment_iban :

enter image description here

With the below code i get a bankAccount with the id 8aa2afbf-f079-44a3-9940-84dd01a97f7f and i added it to an existant assignmentIban with the id 87f6f8ac-fd51-493d-860b-f6975faea472

AssignIbanEntity assignIban = assignmentIbanRepository.findById("87f6f8ac-fd51-493d-860b-f6975faea472");
bankDataEntity bankData = bankDataRepository.findById("8aa2afbf-f079-44a3-9940-84dd01a97f7f");
if (Objects.nonNull(assignIban) && Objects.nonNull(bankData)) {
    assignIban.addBankData(bankData);
    assignmentIbanRepository.save(assignIban);
}

the problem is that the result that i get is not correct :

enter image description here

Expected result:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文