Hibernate映射故障 - 从Y引用X的外键具有错误的列数。应该是0

时间:2016-02-28 22:55:47

标签: hibernate hibernate-mapping

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
}

1 个答案:

答案 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;