私は2つのエンティティクラス User と Address を持っています
@Entity
class User
{
int id;
String name;
}
@Entity
class Vehicle
{
@OneToOne
User user;
String vehName;
}
Vehicle クラスの「user」データ メンバーを主キーとして宣言したいのですが、解決策を教えてもらえますか?
ドキュメントで説明されているように:
最後に、Hibernate に別の関連付けられたエンティティから識別子をコピーするように依頼できます。Hibernate の専門用語では、外部ジェネレーターとして知られていますが、JPA マッピングの方が読みやすく、推奨されています。
@Entity
class MedicalHistory implements Serializable {
@Id @OneToOne
@JoinColumn(name = "person_id")
Person patient;
}
@Entity
public class Person implements Serializable {
@Id @GeneratedValue Integer id;
}
または代わりに
@Entity
class MedicalHistory implements Serializable {
@Id Integer id;
@MapsId @OneToOne
@JoinColumn(name = "patient_id")
Person patient;
}
@Entity
class Person {
@Id @GeneratedValue Integer id;
}