私はそれらの2つのテーブルTeacherとContactを持っています、教師はxのContactsを持つことができます。そこで、ここでは@OneToManyアソシエーションを見ています。
テーブル構造:
ユーザー[userid、username、email、...]
連絡先[contactid、contactname、ref、reftype、...]
ユーザークラスからすべてのユーザーの連絡先をロードしたい。それを行うには、次のようなクエリを実行します
Select * from contact as c WHERE c.ref=8240 AND c.reftype='T';
8240はランダムなユーザーIDであり、reftypeTはTeacher用です。この連絡先テーブルは、学校の連絡先やその他のタイプの顧客にも使用されます。問題は、Hibernateでこれを行う方法がわからないことです。 embedbedIdを使用する必要がありますか?またはJoinColumns?
私がこれまでに行ったことは、私の先生を持っている連絡先にリンクするcontact.ref=teacher.teacherid
ことですが、私が欲しいのは:
contact.ref=teacher.teacherid AND contact.reftype='T'
それ、どうやったら出来るの?
これが私のコードTeacher.classです
private Integer teacherid;
private Set<Contact> contact;
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "teacherid", unique = true, nullable = false)
public Integer getTeacherId() {
return teacherid;
}
@OneToMany(fetch = FetchType.EAGER)
@JoinColumns({
@JoinColumn(name="ref"),
})
public Set<Contact> getContact() {
return contact;
}
public void setContact(Set<Contact> contact) {
this.contact = contact;
}
Contact.class
@Entity
@Table(name = "contact")
public class Contact implements java.io.Serializable {
private Integer contactid;
private String contactname;
private String contacttype;
private String reftype;
private int ref;
/*private Teacher teacher;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumns({
@JoinColumn(name = "ref"),
@JoinColumn(name = "reftype")
})
public Teacher getTeacher() {
return teacher;
}
public void setTeacher (Teacher teacher) {
this.teacher= teacher;
}
*/
private Set<ContactItem> contactItems;
private Set<ContactAddress> contactAddressess;
@OneToMany(fetch=FetchType.EAGER)
@JoinColumn(name="contactid")
public Set<ContactItem> getContactItems(){
return contactItems;
}
public void setContactItems(Set<ContactItem> contactItems) {
this.contactItems = contactItems;
}
@OneToMany(fetch=FetchType.EAGER)
@JoinColumn(name="contactid")
public Set<ContactAddress> getContactAddressess(){
return contactAddressess;
}
public void setContactAddressess(Set<ContactAddress> contactAddressess) {
this.contactAddressess = contactAddressess;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "contactid", unique = true, nullable = false)
public Integer getContactid() {
return this.contactid;
}
public void setContactid(Integer contactid) {
this.contactid = contactid;
}
@Column(name = "contactname", nullable = false)
public String getContactname() {
return this.contactname;
}
public void setContactname(String contactname) {
this.contactname = contactname;
}
@Column(name = "contacttype", nullable = false)
public String getContacttype() {
return this.contacttype;
}
public void setContacttype(String contacttype) {
this.contacttype = contacttype;
}
@Column(name = "reftype", nullable = false, length = 1)
public String getReftype() {
return this.reftype;
}
public void setReftype(String reftype) {
this.reftype = reftype;
}
@Column(name = "ref", nullable = false)
public int getRef() {
return this.ref;
}
public void setRef(int ref) {
this.ref = ref;
}
public String toString(){
return "\n#"+this.contactname+" : ("+this.ref+"-"+this.reftype+") \n"
+"#Items-----\n"+getContactItems()+"\n"
+"#Address---\n"+getContactAddressess()+"\n";
}
}