1

我有这两张表老师和联系人,一个老师可以有 x 个联系人。所以在这里我们正在查看@OneToMany 关联。

表结构:

用户 [userid, username, email,...]
联系人 [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'

我怎么做?

这是我的代码 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;
}

联系人.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";
    }
}
4

2 回答 2

0

假设这Teacher是一个User,并且每个用户都有contacts

用户类

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

//No setContacts here. 

联系人.class

@ManyToOne
private User user;

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

而已。

于 2010-09-21T12:03:49.387 回答
0

首先,因为有一个用户表而没有教师表(教师似乎是用户行的子集,由“类型”列表示)我不会有用户表和教师模型。我将只有一个用户模型。如果您以 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 代码,使维护更加困难。

看看这个的第 3.4 节

于 2010-09-21T12:23:09.200 回答