JPA:许多与许多关系无法正常工作
我有两个以下实体,指令包含许多银行账户,而银行账户属于一个或多个指派。
@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);
}
问题上,问题是我无法正确的结果:
预期结果:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论