java - JPA:在保存父母时重复孩子
问题描述
我正在使用 JPA 来与数据库交互。我有这堂课:
@Entity
@Getter
@Setter
public class ConfigRt {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column
private String uuid;
private String ip;
@Column
private String fiscalCode;
private Boolean active;
private LocalDateTime creationDate;
private LocalDateTime modificationDate;
@OneToMany(mappedBy = "configRt", cascade = CascadeType.ALL)
private List<VatRt> vatRtList;
@OneToMany(mappedBy = "configRt", cascade = CascadeType.ALL)
private List<DepartmentRt> departmentRtList;
@OneToMany(mappedBy = "configRt", cascade = CascadeType.ALL)
private List<PaymentRt> paymentRtList;
@OneToMany(mappedBy = "configRt", cascade = CascadeType.ALL)
private List<OperatorRt> operatorRtList;
@ElementCollection
private List<HeaderRt> header;
@ManyToOne
private SellPoint sellPoint;
@Enumerated(EnumType.STRING)
private RtXmlVersion xmlVersion;
@Column(columnDefinition = "boolean default false")
private boolean enableLottery;
@Column(columnDefinition = "date default '2021-02-01'")
private LocalDate startLotteryDate;
@Enumerated(EnumType.STRING)
private AdvanceType advanceType;
}
我正在使用这个函数来更新数据库中的记录
ConfigRt configRt = new ConfigRt();
// SETTING HERE THE NEW PROPERTIES OF CONFIGRT
configRtRepository.save(configRt)
正在更新,但所有子元素都在数据库表中重复。我做错了什么?
编辑:这里是我用来创建新 ConfigRT 的函数
private ConfigRt reverseMappingConfigRt(ConfigRtDto configRtDto) {
ConfigRt configRt = null != configRtDto.getId() ? configRtRepository.findById(configRtDto.getId()).orElse(new ConfigRt()) : new ConfigRt();
configRt.setId(configRtDto.getId());
configRt.setUuid(configRtDto.getUuid());
configRt.setIp(configRtDto.getIp());
configRt.setFiscalCode(configRtDto.getFiscalCode());
configRt.setActive(configRtDto.getActive());
configRt.setCreationDate(configRtDto.getCreationDate());
configRt.setModificationDate(configRtDto.getModificationDate());
if (configRt.getVatRtList() == null) {
configRt.setVatRtList(new ArrayList<>());
}
configRt.getVatRtList().clear();
configRt.getVatRtList().addAll(configRtDto.getVatList().stream()
.map(v -> reverseMappingVatRt(v, configRt))
.collect(Collectors.toList()));
if (configRt.getPaymentRtList() == null) {
configRt.setPaymentRtList(new ArrayList<>());
}
configRt.getPaymentRtList().clear();
configRt.getPaymentRtList().addAll(configRtDto.getPaymentRtList().stream()
.map(p -> reverseMappingPaymentRt(p, configRt)
)
.collect(Collectors.toList()));
if (configRt.getDepartmentRtList() == null) {
configRt.setDepartmentRtList(new ArrayList<>());
}
configRt.getDepartmentRtList().clear();
configRt.getDepartmentRtList().addAll(configRtDto.getDepartmentRtList().stream()
.map(d -> reverseMappingDepartmentRt(d, configRt))
.collect(Collectors.toList()));
if (configRt.getOperatorRtList() == null) {
configRt.setOperatorRtList(new ArrayList<>());
}
configRt.getOperatorRtList().clear();
configRt.getOperatorRtList().addAll(configRtDto.getOperatorRtList().stream()
.map(o -> reverseMappingOperatorRt(o, configRt))
.collect(Collectors.toList()));
configRt.setHeader(configRtDto.getHeader().stream()
.map(this::reverseMappingHeaderRt)
.collect(Collectors.toList()));
configRt.setXmlVersion(configRtDto.getXmlVersion());
configRt.setAdvanceType(configRtDto.getAdvanceType());
configRt.setEnableLottery(configRtDto.isEnableLottery());
if (null != configRtDto.getStartLotteryDate())
configRt.setStartLotteryDate(configRtDto.getStartLotteryDate());
else
configRt.setStartLotteryDate(LocalDate.of(2021, 2, 1));
return configRt;
}
解决方案
推荐阅读
- java - 如何在 Spring Boot 中捕获错误请求
- c# - c# 在visual studio中调试windows服务
- python - Django:找不到'create_order'的反向
- vb.net - vb.net 在类之间传递字符串
- python - 在数据框中连接多个 for 循环的更好方法
- jquery - 移动 3 级滚动问题
- javascript - Angular 集成 - HTTP 客户端
- html - 为什么 chrome 会覆盖 -webkit-appearance Css 属性?
- python - 使用多处理池调用具有参数化构造函数的类的方法
- linux - 如何将焦点放在xorg中的单个窗口?