首页 > 解决方案 > 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;
}

标签: javaspring-bootjpa

解决方案


推荐阅读