Hibernate
中出现以下错误的原因是什么?
org.hibernate.AnnotationException:引用X.class的外键具有 列数错误。应为0
子实体是;
@Entity
@Table(name="Patient_Details")
public class Patient implements Serializable {
@Id
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name="userID")
private UserAuthentication userAuth;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "patient2")
private List<Insurance> insurances = new ArrayList<>();
// other fields, constructor, getters, setters
}
父母实体;
@Entity
@Table(name = "insurance")
public class Insurance implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long insuranceID;
@ManyToOne
@JoinColumn(name="patientID",nullable=false)
private Patient patient2;
// other fields, constructor, getters, setters
}
答案 0 :(得分:1)
问题在于共享主键。解决方案是使用@MapsId
更改Patient
类中的ID引用;
@Id
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name="userID")
private UserAuthentication userAuth;
以下示例;
@Id
private long userID;
@MapsId
@OneToOne
@JoinColumn(name = "userID")
private UserAuthentication userAuth;