在JPA 2/Hibernate工作的大型和外国钥匙混杂在一起,我遇到麻烦。 我试图在各国和各省建立一个简单的设想:
国家实体:
@Entity
@Table(name = "country")
public class Country extends DomainObjectBase implements Serializable {
@Id
@Basic(optional = false)
@Column(name = "code")
private String code;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "country")
private List<Province> provinces;
}
小学 关键:
@Embeddable
public class ProvincePK implements Serializable {
@Basic(optional = false)
@Column(name = "code")
private String code;
@Basic(optional = false)
@Column(name = "country_code")
private String countryCode;
}
省实体:
@Entity
@Table(name = "province")
public class Province extends DomainObjectBase implements Serializable {
@EmbeddedId
protected ProvincePK provincePK;
@MapsId("country_code")
@JoinColumn(name = "country_code", referencedColumnName = "code", insertable = false, updatable = false)
@ManyToOne(optional = false)
private Country country;
}
这为我设立了正确的表格,但有一个例外:
国家表:
- code PK
- ...
各省
- code PK FK - This is where the problem is its creating a foreign key reference to the country table s code column
- country_code FK This is the only foreign key reference I want
- ...
我如何描绘我的实体/组合关键人物,以便产生我想要的图象? 现在,我可以把任何数据输入该省,因为我国期望该省包含《省法典》。
Thanks for any help.