I have two models.
@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;
}
What JPA/hibernate annotations do I need to put above homeAddress, schoolAddress and students to make the association work?
Of course I’ve tried many things and nothing worked.
For example, setting
@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;
and
@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.
Also tried using mappedBy but that takes a single argument (can’t do mappedBy="student_homeAddress_id, student_schoolAddress_id".
Also looked into moving the JoinColumns to the Student tablet but I am not sure what the annotations should look like for OneToMany and ManyToOne as I have multiple Addresses there which JoinColumns doesn’t make much sense.
The thing that did work but was not creating the associations was having:
@OneToMany(cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH}, fetch = FetchType.EAGER)
@JoinColumn(name="address_id")
@IndexColumn(name="students_index")
protected List<Student> students;
Using this, when storing in the DB the models, the student_homeAddress_id and student_schoolAddress_id were always null even after storing both ends (the Student and Address model).
My thought is that on the Address table there will be 3 extra columns: student_homeAddress_id (the id of the Student in the Student table for the homeAddress), student_schoolAddress_id (the id of the Student in the Student table for the schoolAddress) and students_index (the 0-based location on the students list). That should suffice, correct?
Any ideas?
Thanks a lot!
We tried mael’s suggestion but we couldn’t make it work.
We ended up following this which worked.
In other words, we have
OneToManyrelations:On
Student:On
Address:And on
AddressStudentAssociation:plus an argument to separate the one address from the other (
isHome).Finally, inside
Studentwe havepublic Address getHomeAddress()which traverses theaddresseslist and returns the correct address. We also had to play with the annotations to make it work. Not optimal in general but it works and we already spent too much time trying to make things work. 😐