2 inheritance hibernate jpa joined-subclass
我的问题与更改保留其 ID 的实体的类型非常相似,但我使用的是 InheritanceType.JOINED 而不是 Table_per_class。
这意味着我不会更改任何表,只是创建一个新的子类,其 ID 与超类相同。
总而言之,我有一个 Person 类和一个 Doctor,它扩展了 Person 并具有相同的 id。我需要从数据库中检索一个 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);
}
Run Code Online (Sandbox Code Playgroud)
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());
}
}
}
Run Code Online (Sandbox Code Playgroud)
就像在您链接的问题中一样,答案是“您不能使用 Hibernate API 执行此操作”。
原因其实很清楚——Hibernate 的目标是让持久性尽可能透明,因此,不能让你用持久化对象做一些你不能用普通 Java 做的事情。一旦你创建了一个Person(在普通的java中)的实例,它总是一个Person. 它永远不会是Doctor. 您能做的最好的事情是创建一个Doctor实例并将Person的属性复制到它。
然而,与普通的 Java 不同,使用 Hibernate,您可以作弊并实现您想要的 :-) 但这必须通过本机 SQL 来完成。在您的方法中,您需要:
Person从会话中驱逐实例(如果适用,还有二级缓存)Person实例)的行插入Doctors表中。这是必须作为本机 sql 完成的部分,但您可以将其定义为命名查询并将上述 id 设置为参数。请注意,如果对Doctor属性有任何限制,您需要确保您插入的值满足它们。Person实例 - 现在将加载为Doctor.