我遵循教程mkyong Hibernate - 多对多示例 - 连接表+额外列(注释),以便在我的两个类MachineJob和MachineJobPart之间创建多对多关联。我希望关联的表有额外的列,所以我需要执行以下步骤,
MachineJob表格结构:
CREATE TABLE machine_job
(
id uuid NOT NULL DEFAULT uuid_generate_v4(),
)
MachineJobPart表格结构:
CREATE TABLE machine_job_part
(
id uuid NOT NULL DEFAULT uuid_generate_v4(),
machine_job_id uuid,
part_id uuid,
part_cost double precision,
part_quantity integer,
total_part_cost double precision,
CONSTRAINT machine_job_part_pkey PRIMARY KEY (id),
CONSTRAINT machine_job_part_machine_job_id_fkey FOREIGN KEY (machine_job_id)
REFERENCES machine_job (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT part_tbl_fkey FOREIGN KEY (part_id)
REFERENCES part (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
我的代码运行正常,但问题是machine_job_id没有保存在machine_job_part表中。
@Entity
@Table(name = "machine_job")
public class MachineJob implements Serializable {
@Id
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid2")
@Column(name = "id")
@Type(type = "pg-uuid")
private UUID id;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.machineJob", cascade=CascadeType.ALL)
private Set<MachineJobPart> machineJobParts = new HashSet<MachineJobPart>(0);
@Column(name = "created_date")
private Date createdDate;
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
public void setJobManHoursCost(Double jobManHoursCost) {
this.jobManHoursCost = jobManHoursCost;
}
public Set<MachineJobPart> getMachineJobParts() {
return machineJobParts;
}
public void setMachineJobParts(Set<MachineJobPart> machineJobParts) {
this.machineJobParts = machineJobParts;
}
}
@Entity
@Table(name = "machine_job_part")
@AssociationOverrides({
@AssociationOverride(name = "pk.machineJob",
joinColumns = @JoinColumn(name = "machine_job_id")),
@AssociationOverride(name = "pk.part",
joinColumns = @JoinColumn(name = "part_id"))})
public class MachineJobPart implements Serializable {
private static final long serialVersionUID = 1L;
private MachineJobPartId pk = new MachineJobPartId();
private UUID id;
private double partCost;
private Integer partQuantity;
private Double totalPartCost;
@EmbeddedId
public MachineJobPartId getPk() {
return pk;
}
public void setPk(MachineJobPartId pk) {
this.pk = pk;
}
@Column(name = "part_cost")
public double getPartCost() {
return partCost;
}
public void setPartCost(double partCost) {
this.partCost = partCost;
}
@Transient
public MachineJob getMachineJob() {
return getPk().getMachineJob();
}
public void setMachineJob(MachineJob machineJob) {
getPk().getMachineJob();
}
@Transient
public Part getPart() {
return getPk().getPart();
}
public void setPart(Part part) {
getPk().setPart(part);
}
@GeneratedValue(generator = "system-uuid")
@Column(name = "id", insertable = false, updatable = false)
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
@Column(name = "part_quantity")
public Integer getPartQuantity() {
return partQuantity;
}
public void setPartQuantity(Integer partQuantity) {
this.partQuantity = partQuantity;
}
@Column(name = "total_part_cost")
public Double getTotalPartCost() {
return totalPartCost;
}
public void setTotalPartCost(Double totalPartCost) {
this.totalPartCost = totalPartCost;
}
}
@Embeddable
public class MachineJobPartId implements java.io.Serializable {
private MachineJob machineJob;
private Part part;
@ManyToOne
public MachineJob getMachineJob() {
return machineJob;
}
public void setMachineJob(MachineJob machineJob) {
this.machineJob = machineJob;
}
@ManyToOne
public Part getPart() {
return part;
}
public void setPart(Part part) {
this.part = part;
}
}