几天来,我一直在尝试让复合键工作,查看网络上的许多示例,但由于它不起作用,仍然缺少一些东西。
在下面的代码中 FMX.modelRiskMitigant 和 FMX.modelFacility 被正确设置,但休眠试图在 DB 列中设置 null !
@Entity
@Table(name="FACILITY_MITIGANT_XREF")
@IdClass(FMXPK.class)
public class FMX implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name="FACILITY_ID")
private Long facilityId;
@Id
@Column(name="RISK_MITIGANT_ID")
private Long riskMitigantId;
@ManyToOne
@JoinColumn(name="FACILITY_ID", insertable=false, updatable=false)
private Facility modelFacility;
@ManyToOne
@JoinColumn(name="RISK_MITIGANT_ID", insertable=false, updatable=false)
private RiskMitigant modelRiskMitigant;
}
public class FMXPK implements Serializable {
private static final long serialVersionUID = 1L;
private Long facilityId;
private Long riskMitigantId;
}
@Entity
@Table(name="MODEL_FACILITY")
public class Fac implements Serializable {
private static final long serialVersionUID = 1L;
@OneToMany(mappedBy="modelFacility", fetch=FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval=true)
private Set<FMX> facilityMitigantXrefs;
}
@Entity
@Table(name="MODEL_RISK_MITIGANT")
public class RiskMitigant implements Serializable {
private static final long serialVersionUID = 1L;
@OneToMany(mappedBy="modelRiskMitigant", fetch=FetchType.EAGER, cascade = CascadeType.ALL)
private Set<FMX> facilityMitigantXrefs;
}