私は2つのモデルを持っています。
@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とManyToOneの注釈がどのように表示されるかわかりません。
動作したが、関連付けを作成していなかったのは、次のことでした。
@OneToMany(cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH}, fetch = FetchType.EAGER)
@JoinColumn(name="address_id")
@IndexColumn(name="students_index")
protected List<Student> students;
これを使用して、モデルをDBに保存する場合、student_homeAddress_idとstudent_schoolAddress_idは、両端を保存した後でも常にnullでした(StudentモデルとAddressモデル)。
私の考えでは、Address
テーブルには3つの追加の列があります:student_homeAddress_id(homeAddressのStudentテーブルのStudentのID)、student_schoolAddress_id(schoolAddressのStudentテーブルのStudentのID)、students_index(0 students
-リスト上の場所に基づく)。それで十分でしょう?
何か案は?
どうもありがとう!