【发布时间】:2010-09-21 11:04:39
【问题描述】:
我有这两张表教师和联系人,一个教师可以有 x 个联系人。所以在这里我们正在查看@OneToMany 关联。
表结构:
用户 [用户 ID、用户名、电子邮件、...]
联系人 [contactid, contactname, ref, reftype,...]
我想从我的用户类中加载所有用户的联系人。为此,我会进行类似
的查询Select * from contact as c WHERE c.ref=8240 AND c.reftype='T';
8240 是一个随机用户 ID,引用类型 T 是教师。由于此联系人表也用于学校联系人和/或我们可能拥有的任何其他类型的客户。 问题是我不知道如何使用 Hibernate 来做到这一点。我应该使用 embedbedId 吗?还是 JoinColumns?
到目前为止,我所做的是将我的老师与contact.ref=teacher.teacherid 的联系人联系起来,但我想要的是:
contact.ref=teacher.teacherid AND contact.reftype='T'
我该怎么做?
这是我的代码 老师.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;
}
联系人.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";
}
}
【问题讨论】:
-
teacher是user(如果不是,为什么要显示用户表)?你有某种继承吗?ref列是否有外键约束? -
即使您进行了编辑,我认为我在下面的回答总体上仍然是正确的。您必须更改详细信息以匹配您的情况。
-
个人认为设计有问题。请澄清用户/教师和联系人之间的关系,这很重要。
-
对不起他的困惑.. 让我们称我的 User.class Teacher.class,我在 db 中的表是教师。关于TEacher和Contact之间的关系我想我很清楚-> contact.ref=teacher.teacherid AND contact.reftype='T' 在我的联系表中我没有任何FK,我只是使用索引'ref' ,'reftype' 显然是 PK contactid
标签: java hibernate-annotations hibernate-onetomany