使用Hibernate和JPA进行多列复合键映射

时间:2012-12-07 06:01:09

标签: hibernate jpa hibernate-mapping composite-key

我现在已经尝试了几天让复合键工作,查看网络上的许多示例,但由于它不起作用,我仍然遗漏了一些东西。

在下面的代码中正确设置了FMX.modelRiskMitigant和FMX.modelFacility,但是hibernate试图在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;

}

0 个答案:

没有答案