我有两张桌子,一对一的关系是黑白学生和地址表
Student
student_id
name
Address
address_id
student_id(foreign key to student_id in student table)
...
这是相关实体部分
@Entity
@Table(name = "STUDENT")
public class Student {
@Id
@GeneratedValue
@Column(name = "STUDENT_ID")
private long id;
@OneToOne
private Address address;
}
@Entity
@Table(name = "ADDRESS")
public class Address {
@Id
@GeneratedValue
@Column(name = "ADDRESS_ID")
private long id;
private long student_id;
}
我想将外键(即student_id(foreign key to id in student table)
保留在地址表下,如顶部的表结构所示,但
希望在学生实体下保持private Address address
的无方向关联。我不确定如何实现?
这是我尝试过的
@实体 @Table(名称=“学生”) 公共班学生{
@Id
@GeneratedValue
@Column(name = "STUDENT_ID")
private long id;
@OneToOne(mappedBy ="student_id")
private Address address;
}
@Entity
@Table(name = "ADDRESS")
public class Address {
@Id
@GeneratedValue
@Column(name = "ADDRESS_ID")
private long id;
@OneToOne
@JoinColumn(name="student_id")
private long student_id;
}
但是它给出了以下异常
@OneToOne or @ManyToOne on Address.student_id references an unknown entity: int
at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:107)
at org.hibernate.cfg.Configuration.processEndOfQueue(Configuration.java:1580)
at
只要我与
建立双向关联,它就会起作用 private Student student_id
而不是private long student_id
因此,当补充表(在本例中为地址表)中存在外键时,Hibernate强迫我进行双向关联