休眠映射问题 - 从 Y 引用 X 的外键具有错误的列数.应为 0



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
}

问题出在共享主键上。解决方案是使用@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;

最新更新