私の質問はChanging the type of an entity preserving its IDとよく似ていますが、代わりに私はTable_per_classの代わりにInheritanceType.JOINEDを使用しています。jpa親エンティティと同じIDを持つ新しいエンティティを作成する方法(JOINED継承)
これは、スーパークラスと同じIDを持つ新しいサブクラスを作成するために、テーブルを変更する必要がないことを意味します。
要約すると、私はPersonクラスとPersonを拡張し、同じIDを持つDoctorを持っています。 私は、データベースからPersonを取得し、それをDoctorにして、Personエンティティからのすべてのデータを保存しますが、Doctorのための追加データを作成する必要があります。
医者をマージしようとすると、新しいIDが生成されますが、これは私にとっては無効です。ちょうどあなたがにリンクされてきた問題のように、答えは「あなたが使用してこれを行うことはできませんで最初の場所で
private Person getDoctor(Person person) {
// Person already a doctor ==> OK
if (person instanceof Doctor) {
return person;
}
// Transient Person ==> //Transient Doctor OK
if (person == null) {
return new Doctor();
}
// Creates a Doctor from the person (only setting id...),
// and merges it ==>
fails as the id changes.
Doctor doctor = new Doctor(person);
return personDAO.merge(doctor);
}
sorry guys,first time here.
Here´s the code above:
private Person getDoctor(Person person) {
//Person already a doctor ==> OK
if (person instanceof Doctor) {
return person;
}
//Transient Person ==> //Transient Doctor OK
if (person == null) {
return new Doctor();
}
//Creates a Doctor from the person (only setting id...), and merges it ==> fails as the id changes.
Doctor doctor = new Doctor(person);
return personDAO.merge(doctor);
}
@Inheritance(strategy = InheritanceType.JOINED)
@Entity
public class Person{
}
@Entity
public class Doctor extends Person{
public Doctor(Person person) {
if (person != null) {
this.setId(person.getId());
}
}
}