标签:hibernate
基本的需求:在一个部门(Department)里面有多个员工(Employee),符合1:N的关系。
在05节当中,使用了最基础的类型String。而在本节当中,使用用户自定义的Department类和Employee类,不过,这也正是需要重点讨论的问题。
本节的重点是:自定义的类型的映射 和 1:N关系的映射。
其中涉及到<one-to-many>和<many-to-one>标签,把握它们的本质要抓住4个概念:表(table)、字段(column)、类(Class)和属性(property)。
Department.java
package com.rk.hibernate.g_one2Many;
import java.util.Set;
public class Department
{
private int deptId;
private String deptName;
private Set<Employee> emps;
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;
}
} Department.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.rk.hibernate.g_one2Many" auto-import="true"> <class name="Department" table="T_Department"> <id name="deptId" column="id"> <generator class="native"></generator> </id> <property name="deptName" column="name" type="string"></property> <set name="emps" table="T_Employee"> <key column="deptId"></key> <one-to-many class="Employee"/> </set> </class> </hibernate-mapping>
Employee.java
package com.rk.hibernate.g_one2Many;
public class Employee
{
private int empId;
private String empName;
private double salary;
private Department 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 double getSalary()
{
return salary;
}
public void setSalary(double salary)
{
this.salary = salary;
}
public Department getDept()
{
return dept;
}
public void setDept(Department dept)
{
this.dept = dept;
}
}Employee.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.rk.hibernate.g_one2Many" auto-import="true"> <class name="Employee" table="T_Employee"> <id name="empId" column="id"> <generator class="native"></generator> </id> <property name="empName" column="name"></property> <property name="salary" column="salary"></property> <many-to-one name="dept" column="deptId" class="Department"></many-to-one> </class> </hibernate-mapping>
App.java
package com.rk.hibernate.g_one2Many;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
public class App1
{
private static SessionFactory sf;
static
{
sf = new Configuration()
.configure()
.addClass(Department.class)
.addClass(Employee.class)
.buildSessionFactory();
}
// 保存【由Department来维护Department和Employee的关系】
@Test
public void testSaveDepartment()
{
Session session = sf.openSession();
session.beginTransaction();
//员工1
Employee emp1 = new Employee();
emp1.setEmpName("张三");
emp1.setSalary(1000);
//员工2
Employee emp2 = new Employee();
emp2.setEmpName("李四");
emp2.setSalary(800);
//部门
Department dept = new Department();
dept.setDeptName("开发部门");
Set<Employee> emps = new HashSet<Employee>();
emps.add(emp1);
emps.add(emp2);
dept.setEmps(emps);//在部门上建立Department和Employee的关系
//保存
//注意:三者都要进行保存。
//如果仅保存dept对象,dept只是维护了部门和员工之间关系,
//Hibernate并不会将emp1和emp2也进行保存
session.save(dept);
session.save(emp1);
session.save(emp2);
session.getTransaction().commit();
session.close();
System.out.println("执行结束!");
}
// 【推荐使用这种方法】 【由Employee来维护Department和Employee之间的关系】
@Test
public void testSaveEmployee()
{
Session session = sf.openSession();
session.beginTransaction();
//部门
Department dept = new Department();
dept.setDeptName("办公室");
//员工1
Employee emp1 = new Employee();
emp1.setEmpName("小明");
emp1.setSalary(1000);
emp1.setDept(dept);//由员工来维护Department和Employee之间的关系
//员工2
Employee emp2 = new Employee();
emp2.setEmpName("小红");
emp2.setSalary(800);
emp2.setDept(dept);//由员工来维护Department和Employee之间的关系
//保存
//部门和员工的关系是1:N,先保存代表1的那一方,再保存代表N的那一方
session.save(dept);
session.save(emp1);
session.save(emp2);
session.getTransaction().commit();
session.close();
System.out.println("执行结束!");
}
@Test
public void testGetDepartment()
{
Session session = sf.openSession();
session.beginTransaction();
Department dept = (Department) session.get(Department.class, 2);
System.out.println(dept.getDeptId());
System.out.println(dept.getDeptName());
System.out.println(dept.getEmps());// 通过部门来获取员工,懒加载
session.getTransaction().commit();
session.close();
System.out.println("执行结束!");
}
@Test
public void testGetEmployee()
{
Session session = sf.openSession();
session.beginTransaction();
Employee emp = (Employee) session.get(Employee.class, 3);
System.out.println(emp.getEmpId());
System.out.println(emp.getEmpName());
System.out.println(emp.getSalary());
System.out.println(emp.getDept().getDeptName());// 通过员工,获取部门信息,懒加载
session.getTransaction().commit();
session.close();
System.out.println("执行结束!");
}
}标签:hibernate
原文地址:http://lsieun.blog.51cto.com/9210464/1813907