我正在开发Java桌面应用程序并使用JPA进行持久化.我有一个问题如下:
我有两个实体:
国家/地区具有以下属性:
City具有以下属性:
现在因为在两个不同的国家/地区可以有两个同名的城市,因此数据库中的City表的primaryKey是由CityName和组成的复合主键CountryName.
现在的问题是如何实现的主键
City作为一个Entity在Java中
@Entity
public class Country implements Serializable {
private String countryName;
@Id
public String getCountryName() {
return this.countryName;
}
}
@Entity
public class City implements Serializable {
private CityPK cityPK;
private Country country;
@EmbeddedId
public CityPK getCityPK() {
return this.cityPK;
}
}
@Embeddable
public class CityPK implements Serializable {
public String cityName;
public String countryName;
}
Run Code Online (Sandbox Code Playgroud)
现在我们知道上面代码中的from Country和Cityis OneToMany之间的关系,我 …