2 attributes in a table mapped by a reference table's attribute
I need your help please.
I'm trying to map my objects with hibernate and jpa.
I have 2 tables , reference and person ( for example)
the attributs of reference are ( id_ref, description_ref)
the attributes of person are : id_person,addresse,colorEyes,jobetc).
and address is a string , but all the others are int. now the 2 int values in colorEyes and job refers to 2 differents int in the reference table with
person.colorEyes=reference.id_ref and person.job=reference.id_ref , those id_ref are not the same for colorEyes and job.
I would like to map those relations between reference and person table , is it possible to do that with hibernate and jpa annotation? should I put two relations of oneToOne type?even if both the attributes are concerned by a reference table ? I found开发者_开发技巧 the selectedtable annotation but they always talk about a different table not always the same between the two attributes..
If it's not possible is there anyway to use alias? or should I go for HQL?
anything will be very helpful.tanks
I would not think Hibernate would have any problem with the following...
@ManyToOne( ...)
@JoinColumn(name="colorEyes")
public Reference getColorEyes() {
return company;
}
@ManyToOne( ...)
@JoinColumn(name="jobetc")
public Reference getJobEtc() {
return company;
}
Notice that I used a ManyToOne not OneToOne as I assume the multiple people could have the same eye color.
However, you might want to really consider splitting your reference table into multiple tables, one for each type of data. This would be better DB form Third Normal Form
This would also make your code more readable as you would have different classes for holding EyeColor and JobEtc.
精彩评论