Orgnization.java
- Java code
package com.chk.model;import java.util.HashSet;import java.util.Set;import javax.persistence.Entity;import javax.persistence.FetchType;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.JoinColumn;import javax.persistence.ManyToOne;import javax.persistence.OneToMany;@Entitypublic class Orgnization { private int id; private String name; private String sn; private String description; private Orgnization parent;// 父节点 private Set<Person> persons = new HashSet<Person>();// 子节点 @Id @GeneratedValue public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSn() { return sn; } public void setSn(String sn) { this.sn = sn; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } @ManyToOne public Orgnization getParent() { return parent; } public void setParent(Orgnization parent) { this.parent = parent; } @OneToMany(mappedBy = "org") public Set<Person> getPersons() { return persons; } public void setPersons(Set<Person> persons) { this.persons = persons; }}
Person.java
- Java code
package com.chk.model;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.ManyToOne;@Entitypublic class Person { private int id; private String name; private String sex; private String address; private String duty; private String phone; private String description; private Orgnization org; @ManyToOne public Orgnization getOrg() { return org; } public void setOrg(Orgnization org) { this.org = org; } @Id @GeneratedValue public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getDuty() { return duty; } public void setDuty(String duty) { this.duty = duty; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; }}
一、我要用Annotation的方式,
二、我现在只能通过 “子”找到“父”
问题是:我如何通过 “父”找到“子”
我的测试时代码如下:
test.java
- Java code
@Test public void loadtest() { Configuration cfg = new Configuration().configure(); SessionFactory sf = cfg.buildSessionFactory(); Session s = sf.getCurrentSession(); s.beginTransaction(); Orgnization org1 = (Orgnization) s.get(Orgnization.class, 1); Set set = org1.getPersons(); System.out.println(set.size()); for (Iterator iterator = set.iterator(); iterator.hasNext();) { Orgnization org = (Orgnization) iterator.next(); System.out.println(org.getName()); } s.getTransaction().commit(); }