我有两个模型。
@Entity
public class Student
{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
protected long id;
@?
protected Address homeAddress;
@?
protected Address schoolAddress;
}
@Entity
public class Address
{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
protected long id;
@?
protected List<Student> students;
}
我需要在上面放置哪些 JPA/hibernate 注释homeAddress
,schoolAddress
并使students
关联工作?
当然,我尝试了很多东西,但没有任何效果。例如,设置
@ManyToOne
@JoinColumn (name="student_homeAddress_id", updatable = false, insertable = false)
protected Address homeAddress;
@ManyToOne
@JoinColumn (name="student_schoolAddress_id", updatable = false, insertable = false)
protected Address schoolAddress;
和
@OneToMany(cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH}, fetch = FetchType.EAGER)
@JoinColumns({
@JoinColumn(name = "student_homeAddress_id", referencedColumnName = "student_homeAddress_id"),
@JoinColumn(name = "student_schoolAddress_id", referencedColumnName = "student_schoolAddress_id")})
@IndexColumn(name="students_index")
protected List<Student> students;
耶Unable to find column with logical name: student_homeAddress_id in org.hibernate.mapping.Table(Address) and its related supertables and secondary tables
。也尝试过使用mappedBy
,但这需要一个参数(不能做mappedBy="student_homeAddress_id, student_schoolAddress_id"
.
还考虑将其JoinColumns
移至Student
平板电脑,但我不确定 OneToMany 和 ManyToOne 的注释应该是什么样子,因为我在那里有多个地址,而 JoinColumns 没有多大意义。
确实有效但没有创建关联的事情是:
@OneToMany(cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH}, fetch = FetchType.EAGER)
@JoinColumn(name="address_id")
@IndexColumn(name="students_index")
protected List<Student> students;
使用这个,当在数据库中存储模型时,student_homeAddress_id 和 student_schoolAddress_id 始终为空,即使在存储了两端(学生和地址模型)之后也是如此。
我的想法是在Address
桌子上会有3个额外的列:student_homeAddress_id(学生表中学生的家庭地址的id),student_schoolAddress_id(学生表中学生的id)和students_index(0 -基于students
列表中的位置)。这应该足够了,对吗?
有任何想法吗?
非常感谢!