多对一映射与一对多映射:
使用部门和员工的关系来说明问题.一个部门可以有多个员工,但是一个员工只属于一个部门.
开发步骤:
(1)JavaBean的设计,Dept(部门表) Employee(员工表)
(2)映射关系
(3)测试
1.Dept和Employee Javabean
Dept
package cn.itcast.one2many;
import java.util.HashSet;
import java.util.Set;
public class Dept {
private int deptId;
private String deptName;
private Set<Employee> emps=new HashSet<Employee>();
public int getDeptId() {
return deptId;
}
public void setDeptId(int deptId) {
this.deptId = deptId;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
public Set<Employee> getEmps() {
return emps;
}
public void setEmps(Set<Employee> emps) {
this.emps = emps;
}
}
Employee
package cn.itcast.one2many;
public class Employee {
private int empId;
private String empName;
private Dept dept;
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public Dept getDept() {
return dept;
}
public void setDept(Dept dept) {
this.dept = dept;
}
}
2.映射文件
Dept.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.itcast.one2many" auto-import="true">
<class name="Dept" table="t_dept">
<id name="deptId">
<generator class="native"></generator>
</id>
<property name="deptName" column="deptName" length="20" type="string"></property>
<span style="color:#ff0000;"><!--
一对多的关系
dept映射关键点
1.指定映射的集合属性
2.集合属性对应的集合表
3.集合表的外键字段 "dept_id"
4.集合元素的类型Set<>尖括号里面的
--></span>
<set name="emps" table="t_employee">
<key column="dept_id"></key>
<one-to-many class="Employee"/>
</set>
</class>
</hibernate-mapping>
Employee.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.itcast.one2many" auto-import="true">
<class name="Employee" table="t_employee">
<id name="empId">
<generator class="native"></generator>
</id>
<property name="empName" column="empName" length="20" type="string"></property>
<span style="color:#ff0000;"><!--
多对一:
1.映射的部门属性:dept
2.映射部门的属性,对应的外键字段
3.部门的类型
--></span>
<many-to-one name="dept" column="dep_id" class="Dept"></many-to-one>
</class>
</hibernate-mapping>
3.测试类
package cn.itcast.one2many;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;
import org.junit.Test;
import cn.itcast.utils.HibernateUtil;
public class App {
@Test
public void testOne2Many(){
Session session = HibernateUtil.getSession();
Transaction transaction = session.beginTransaction();
//通过部门维护员工
//1.部门
Dept dept=new Dept();
dept.setDeptName("行政部");
//2.员工
Employee emp_lwk=new Employee();
emp_lwk.setEmpName("李卫康");
Employee emp_hw=new Employee();
emp_hw.setEmpName("韩玮");
//3.关系
dept.getEmps().add(emp_lwk);
dept.getEmps().add(emp_hw);
//4.保存到数据库要保存每一个对象
session.save(emp_hw);
session.save(emp_lwk);
session.save(dept);
transaction.commit();
session.close();
}
}
运行测试可以发现数据库中的表数据:
可以发现t_employee表中的外键约束