Hibernate_映射_关联关系_一对多多对一映射1

来源:互联网 发布:少儿绘画的软件下载 编辑:程序博客网 时间:2024/05/22 10:50
值类型的集合集合元素是普通类型实体类型的集合集合元素是另外一个实体一对多     多对一Department.hbm.xmlEmployee.hbm.xml要说明的信息有:要说明的信息有:1,集合表(员工表)1,关联的是什么类型2,集合外键2,外键列3,集合元素 关联的实例类型


部门映射文件

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!-- 导入包 --><hibernate-mapping package="cn.itcast.f_hbm_oneToMany"><!-- 类名 --><class name="Department" table="department"><id name="id" type="integer" column="id_"><generator class="native" /></id><property name="name" type="string" column="name_" /><!-- employee属性,Set集合,表达的是本类与Employee的一对多关系 --><set name="employee"><key column="departmentId" /><one-to-many class="Employee" /></set></class></hibernate-mapping>
员工映射文件

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!-- 导入包 --><hibernate-mapping package="cn.itcast.f_hbm_oneToMany"><!-- 类名 --><class name="Employee" table="employee"><id name="id" type="integer" column="id_"><generator class="native" /></id><property name="name" type="string" column="name_" /><!-- department属性,表达的是本类与Department的多对一的关系 --><many-to-one name="department" class="Department" column="departmentId"></many-to-one></class></hibernate-mapping>
部门实例

package cn.itcast.f_hbm_oneToMany;import java.util.HashSet;import java.util.Set;/** * 部门 *  * @author 风清杨 * @version V3.0 *  */public class Department {/** 部门id */private Integer id;/** 部门名称 */private String name;/** 员工信息 */private Set<Employee> employee = new HashSet<Employee>();// 关联的很多员工public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Set<Employee> getEmployee() {return employee;}public void setEmployee(Set<Employee> employee) {this.employee = employee;}@Overridepublic String toString() {return "Department [id=" + id + ", name=" + name + "]";}}
员工实例

package cn.itcast.f_hbm_oneToMany;/** * 员工部门 *  * @author 风清杨 * @version V3.0 *  */public class Employee {/** 员工编号 */private Integer id;/** 员工姓名 */private String name;/** 员工部门 */private Department department;// 关联的部门对象public Integer getId() {return id;}public void setId(Integer 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;}@Overridepublic String toString() {return "Employee [id=" + id + ", name=" + name + "]";}}

测试类

package cn.itcast.f_hbm_oneToMany;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.junit.Test;/** * 应用程序操作类 *  * @author 风清杨 * @version V3.0 *  */public class App {private static SessionFactory sessionFactory = new Configuration()//.configure()//.addClass(Department.class)//.addClass(Employee.class)//.buildSessionFactory();// 保存,有关联关系@Testpublic void testSave() throws Exception {Session session = sessionFactory.openSession();Transaction tx = null;try {tx = session.beginTransaction();// ------------------------------------// 新建对象Department department = new Department();department.setName("开发部");Employee employee1 = new Employee();employee1.setName("张三");Employee employee2 = new Employee();employee2.setName("李四");// 关联起来employee1.setDepartment(department);employee2.setDepartment(department);department.getEmployee().add(employee1);department.getEmployee().add(employee2);// 保存session.save(department);session.save(employee1);session.save(employee2);// ------------------------------------tx.commit();} catch (RuntimeException e) {tx.rollback();throw e;} finally {session.close();}}// 获取,可以获取到关联的对方@Testpublic void testGet() throws Exception {Session session = sessionFactory.openSession();Transaction tx = null;try {tx = session.beginTransaction();// ------------------------------------// 获取一方数据,并显示另一方信息//Department department = (Department) session.get(Department.class,// 1);// System.out.println(department);// System.out.println(department.getEmployee());Employee employee = (Employee) session.get(Employee.class, 1);System.out.println(employee);System.out.println(employee.getDepartment());// ------------------------------------tx.commit();} catch (RuntimeException e) {tx.rollback();throw e;} finally {session.close();}}}





0 0