Hibernate - OneToMany - 几列

时间:2010-09-21 11:04:40

标签: java hibernate-annotations hibernate-onetomany

我有2个表教师和联系人,教师可以有x个联系人。所以我们在这里看一个@OneToMany协会。

表格结构:

  

用户[用户ID,用户名,电子邮件,...]
  联系[contactid,contactname,ref,reftype,...]

我想从我的用户类加载所有用户的联系人。要做到这一点,我会做一个像

这样的查询
Select * from contact as c WHERE c.ref=8240 AND c.reftype='T';

8240是一个随机用户ID,并且reftype T代表教师。由于此联系表也用于学校联系人和/或我们可能拥有的任何其他类型的客户。 问题是我不知道怎么用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";
    }
}

2 个答案:

答案 0 :(得分:0)

假设TeacherUser,并且每个用户都有contacts

User.class

@OneToMany(mappedBy = "user", targetEntity = Contact.class, orphanRemoval=true)
@Cascade(CascadeType.ALL)
private Set<Contact> contacts = new ConcurrentSkipListSet<Contact>();

//No setContacts here. 

Contact.class

@ManyToOne
private User user;

public void setUser(User user){
this.user = user;
}

就是这样。

答案 1 :(得分:0)

首先,由于有一个User表而没有Teacher表(教师似乎是用户行的子集,用'type'列表示),我没有User表和Teacher模型。我只会使用用户模型。如果你以Hibernate的方式做事情,Hibernate会更容易 ,这是每个表的一个模型,模型具有相同的名称。例如,如果执行此操作,则可以使用工具自动生成(反向工程)所有模型类。这意味着Hibernate工具将查看您的表,外键等,并为您的表生成适当的Java代码。当您开始更改表格时非常方便。

通常,您将对模型类进行反向工程。由于这些是机器生成的,因此您不希望更改它们,因为下次更改将被覆盖以对模型进行反向工程。我为你这样的条件所做的是创建一个名为DAO的类 - 数据访问对象或DAO。

public class UserDAO {
    public static User getTeacher(EntityManager em, Long id) {
        try {
        IForgotTheType query = em.createQuery("User from User user, Contact contact where contact.ref=user.teacherid AND contact.reftype='T' and User.id=:id");
        query.setParameter("id", id);
        return (User) query.getSingleResult();
        } catch (NoResultException e) {
           return null;
        } catch (Exception e) {
           throw new RuntimeException(e);
        }
} 

显然我不确定你的表结构和列名,但你明白了。您可以在上面的查询中看到我将代码插入的位置。现在,只需调用UserDAO.getTeacher()即可获得教师。使用DAO - 否则你的代码中的Hibernate代码无处不在会使维护变得更加困难。

Check out section 3.4 of this