package com.zchen.user.domain;
import java.util.HashSet;
import java.util.Set;
public class Department {
private int id;
private String name;
private Set<Employee> employees = new HashSet<Employee>();
public Set<Employee> getEmployees() {
return employees;
}
public void setEmployees(Set<Employee> employees) {
this.employees = employees;
}
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;
}
}
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.zchen.user.domain"> <class name="Department" table="`department`"> <id name="id" column="`id`"> <generator class="native"> </generator> </id> <property name="name" unique="true"/> <set name="employees"> <key column="depart_id"></key> <!-- 外键 --> <one-to-many class="Employee"/> </set> </class> </hibernate-mapping>
package com.zchen.user.domain;
public class Employee {
private int id;
private String name;
private Department department;
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 Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
}
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.zchen.user.domain"> <class name="Employee" table="`employee`"> <id name="id" column="`id`"> <generator class="native"> </generator> </id> <property name="name"/> <many-to-one name="department" column="depart_id"></many-to-one> </class> </hibernate-mapping>
测试:
package test; import java.util.HashSet; import java.util.Set; import org.hibernate.Session; import org.hibernate.Transaction; import com.zchen.user.dao.HibernateUtil; import com.zchen.user.domain.Department; import com.zchen.user.domain.Employee; public class one2Many { public static void main(String[] args) { add(); query(1); } public static Department add(){ Department depart = new Department(); Employee emp1 = new Employee(); Employee emp2 = new Employee(); Session session = null; Transaction tx = null; try { depart.setName("事业部"); emp1.setDepartment(depart);//对象模型 建立两个对象的关联 emp1.setName("阿里郎"); emp2.setDepartment(depart);//对象模型 建立两个对象的关联 emp2.setName("草原狼"); /*Set<Employee> emps = new HashSet<Employee>(); emps.add(emp1); emps.add(emp2); depart.setEmployees(emps);*/ session = HibernateUtil.getSession(); tx = session.beginTransaction(); //session.save(depart); //session.save(emp); session.save(emp1); session.save(emp2); session.save(depart); tx.commit(); } catch (Exception e) { e.printStackTrace(); }finally{ if(session != null){ session.close(); } } return depart; } static Department query(int departId){ Session session = null; Transaction tx = null; Department depart = null; try { session = HibernateUtil.getSession(); tx = session.beginTransaction(); depart = (Department)session.get(Department.class, departId); System.out.println("emp size"+depart.getEmployees().size()); tx.commit(); } catch (Exception e) { e.printStackTrace(); }finally{ if(session != null){ session.close(); } } return depart; } }