多個@OneToMany我有兩個型號。休眠(JPA)爲同一型號
@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;
yealds 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
平板電腦,但我不知道註釋應該像對一對多和多對一什麼,因爲我有多個地址有哪些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(StudentAddress的Student表中Student的id),student_schoolAddress_id(StudentAddress的Student表中的Student的id)和students_index(students
列表中基於0的位置)。這應該就夠了,對嗎?
任何想法?
非常感謝!