私の質問は、その ID を保持するエンティティのタイプを変更することに非常に似ていますが、代わりに、Table_per_class の代わりに InheritanceType.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());
}
}
}