我是Hibernate的新手,并且学习所有不同的注释有时会令人沮丧。目前,我仍然坚持让Doctor扩展Person,并且在Doctor和Specialty之间也有一对一的关系。我已经坚持了一段时间,仍然无法想象这一个。我已经尝试过测试两种关系中的一种,我的代码运行正常,但是当我将所有内容放在一起时遇到问题。
这是我得到的错误:
线程“main”中的异常org.hibernate.MappingException:不能 确定类型:edu.cs157b.medicalSystem.Specialty,在表中: 列,列:[org.hibernate.mapping.Column(special)]
医生:
package edu.cs157b.medicalSystem;
import javax.persistence.*;
@Entity
public class Doctor extends Person {
@OneToOne
@JoinColumn(name = "SPECIALTY_ID")
private Specialty specialty;
private double salary;
public void setSalary(double salary) {
this.salary = salary;
}
public double getSalary() {
return salary;
}
public void setSpecialty(Specialty specialty) {
this.specialty = specialty;
}
public Specialty getspecialty() {
return specialty;
}
}
特长:
package edu.cs157b.medicalSystem;
import javax.persistence.*;
@Entity
public class Specialty {
@OneToOne
private Doctor doctor;
@Id
@GeneratedValue
@Column(name = "SPECIALTY_ID")
private int sId;
private String specialtyTitle;
public void setSId(int sId) {
this.sId = sId;
}
public int getSId() {
return sId;
}
public void setSpecialtyTitle(String specialtyTitle) {
this.specialtyTitle = specialtyTitle;
}
public String getSpecialtyTitle() {
return specialtyTitle;
}
public void setDoctor(Doctor doctor) {
this.doctor = doctor;
}
public Doctor getDoctor() {
return doctor;
}
}
人:
package edu.cs157b.medicalSystem;
import javax.persistence.*;
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class Person {
private int personId;
private String first_name;
public Person() {
}
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "PERSON_ID")
public int getPersonId() {
return personId;
}
public void setPersonId(int personId){
this.personId = personId;
}
public void setFirstName(String first_name) {
this.first_name = first_name;
}
public String getFirstName() {
return first_name;
}
}
答案 0 :(得分:3)
您的代码中有两个错误。
首先,您在Person中注释了getter,并在其子类Doctor中注释了该字段。这就是你得到这个错误的原因:一旦Hibernate在基类的getter上看到@Id
注释,它只会在类层次结构的其余部分考虑getter上的注释,并忽略字段上的注释。
其次,您的OneToOne双向关联映射不正确。一方必须始终是双向关联的反面。那么,以下字段:
@OneToOne
private Doctor doctor;
应该是
@OneToOne(mappedBy = "specialty")
private Doctor doctor;
告知JPA Specialty.doctor
关联是已在Doctor.specialty
中声明和映射的OneToOne关联的反面。