我试图将作为主键的父类ID保存为子类中的外键,但它始终为null。 如果外键与父表中的现有记录不匹配,则DB Schema正确创建了我尝试手动添加的外键约束,这会导致约束错误。
父类:
package com.canaldigital.tsi.order.domain;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import javax.persistence.CascadeType;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
@Entity(name = "Order_Product")
public class NewCustomerProduct extends TrackedSubEntity.Many<Order> {
@Id
@SequenceGenerator(name = "ProductId_SEQ", sequenceName = "ProductId_SEQ", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ProductId_SEQ")
private Long id;
@ElementCollection(fetch = FetchType.LAZY)
@CollectionTable(name = "Product_Offer", joinColumns = @JoinColumn(name = "OrderProduct_Id") )
@Column(name = "Offer")
private List<String> offerIds;
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "prefDef")
private UpgradeDowngrade upgradeDowngrade;
protected NewCustomerProduct() {
}
public NewCustomerProduct(List<String> offerIds, List<String> serialNumbers, UpgradeDowngrade upgradeDowngrade) {
this.offerIds = offerIds;
this.upgradeDowngrade = upgradeDowngrade;
}
private NewCustomerProduct(Builder builder) {
offerIds = builder.offerIds;
upgradeDowngrade = builder.upgradeDowngrade;
}
public static Builder create() {
return new Builder();
}
public Builder copy() {
return new Builder(this);
}
public List<String> getOfferIds() {
return Collections.unmodifiableList(offerIds);
}
public Optional<UpgradeDowngrade> getUpgradeDowngrade() {
return Optional.ofNullable(upgradeDowngrade);
}
public void setUpgradeDowngrade(UpgradeDowngrade upgradeDowngrade) {
this.upgradeDowngrade = upgradeDowngrade;
}
public static final class Builder {
private List<String> serialNumbers = new ArrayList<>();
private List<String> offerIds = new ArrayList<>();
private UpgradeDowngrade upgradeDowngrade;
private Builder() {
}
private Builder(NewCustomerProduct newCustomerProduct) {
offerIds = new ArrayList<>(newCustomerProduct.offerIds);
upgradeDowngrade = newCustomerProduct.upgradeDowngrade;
}
public Builder withOfferId(String offerId) {
this.offerIds.add(offerId);
return this;
}
public Builder withSerialNumbers(String serialNumbers) {
this.serialNumbers.add(serialNumbers);
return this;
}
public NewCustomerProduct build() {
return new NewCustomerProduct(this);
}
public Builder withUpgradeDowngrade(UpgradeDowngrade upgradeDowngrade) {
this.upgradeDowngrade = upgradeDowngrade;
return this;
}
}
}
儿童班:
package com.canaldigital.tsi.order.domain;
import java.time.LocalDate;
import java.util.Optional;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
@Entity
@Table(name = "Upgrade_Downgrade_Customer_Product")
public class UpgradeDowngrade {
@Id
@SequenceGenerator(name = "UpgradeDowngradeId_SEQ", sequenceName = "UpgradeDowngradeId_SEQ", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "UpgradeDowngradeId_SEQ")
private Long id;
@Column(name = "Old_Commercial_Product_Id")
private Integer oldCommercialProductId;
@Column(name = "Upgrade")
private Boolean upgrade;
@Column(name = "Schedule_Date")
private LocalDate scheduleDate;
@OneToOne
@JoinColumn(name = "OrderProduct_Id")
private NewCustomerProduct prefDef;
public UpgradeDowngrade(Integer oldCommercialProductId, boolean upgrade, LocalDate scheduleDate) {
this.oldCommercialProductId = oldCommercialProductId;
this.upgrade = upgrade;
this.scheduleDate = scheduleDate;
}
public UpgradeDowngrade() {
// Required by Jackson
}
public Optional<Integer> getOldCommercialProductId() {
return Optional.ofNullable(oldCommercialProductId);
}
public Optional<Boolean> isUpgrade() {
return Optional.ofNullable(upgrade);
}
public Optional<LocalDate> getScheduleDate() {
return Optional.ofNullable(scheduleDate);
}
}