私はJavaデスクトップアプリケーションを開発しており、JPAを永続化に使用しています。以下の問題があります。
私は2つのエンティティを持っています:
- 国
- 街
国には次の属性があります。
- 国名 (PK)
City には次の属性があります。
- 都市名
2 つの異なる国に同じ名前の 2 つの都市が存在する可能性があるため、データベース内の City テーブルの primaryKey は、CityNameとで構成される複合主キーCountryNameです。
今私の質問は、主キーをJavaで実装する
City方法Entityです
@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;
}
Countryからへの関係がわかっているのでCity、OneToMany上記のコードでこの関係を示すために、クラスにcountry変数を追加しました。City
しかし、クラスのオブジェクトcountryName内の 2 つの場所に重複した data( ) が格納されています。1 つはオブジェクト内、もう1 つはオブジェクト内です。CitycountrycityPK
しかし一方で、両方が必要です。
countryNamecityPKこのように複合主キーを実装するため、in object が必要です。countryNameincountryobject は、オブジェクト間の関係を示す標準的な方法であるため、必要です。
この問題を回避するにはどうすればよいですか?