Mam dwa modele.Hibernacja (JPA) wiele @OneToMany dla tego samego modelu
@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;
}
Co WZP/hibernacji adnotacje muszę umieścić powyżej homeAddress
schoolAddress
i students
, aby uczynić pracę stowarzyszenia?
Oczywiście próbowałem wielu rzeczy i nic nie działało. Na przykład, ustawienie
@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;
i
@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
. próbował również przy użyciu mappedBy
ale że przyjmuje jeden argument (nie można zrobić mappedBy="student_homeAddress_id, student_schoolAddress_id"
.
również wyglądał na przesuwanie JoinColumns
do tabletu Student
ale nie jestem pewien, co adnotacje powinny wyglądać dla OneToMany i ManyToOne jak mam wiele adresów, które JoinColumns tam nie ma sensu
rzecz, która nie działa, ale nie było stworzenie stowarzyszenia był o.
@OneToMany(cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH}, fetch = FetchType.EAGER)
@JoinColumn(name="address_id")
@IndexColumn(name="students_index")
protected List<Student> students;
Używanie tego, podczas przechowywania w DB modelach student_homeAddress_id i student_schoolAddress_id były zawsze puste, nawet po zapisaniu obu końców (model Studenta i Adresu).
Moja myśl jest taka, że na stole Address
będą 3 dodatkowe kolumny: student_homeAddress_id (identyfikator studenta w tabeli Studenta dla homeAddress) student_schoolAddress_id (identyfikator studenta w tabeli Studenta dla schoolAddress) i students_index (lokalizacja oparta na 0 na liście students
). To powinno wystarczyć, prawda?
Wszelkie pomysły?
Wielkie dzięki!