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
- Hibernate_映射_关联关系_一对多多对一映射1
- Hibernate_映射_关联关系_一对多多对一映射2、inverse属性
- Hibernate_映射_关联关系_多对多映射1
- Hibernate_映射_关联关系_一对一映射1__映射基础
- Hibernate_映射_关联关系_多对多映射2
- Hibernate_一对多_多对一_双向关联
- Hibernate_映射_关联关系_cascade属性、单向关联
- Hibernate_映射_关联关系_一对一映射2_基于外键的方式
- Hibernate_映射_关联关系_一对一映射3_基于主键的方式
- Hibernate关系映射(7)_多对一单向关联
- Hibernate关联关系映射_单边多对一
- Hibernate关联关系映射_多对一双边
- Hibernate关系映射(2)_一对一双向外键关联
- Hibernate关系映射(8)_一对多单向关联
- Hibernate关联关系映射_单边一对多
- Hibernate_多对一单向关联_一对多单向关联
- Hibernate之关联关系映射(一对多和多对一映射,多对多映射)
- Hibernate关联关系映射 一对一 一对多 多对一
- Idea 中JDK版本设置问题导致ClassNotFoundException错误的一种原因
- C、C++基础知识之 定义指针
- 简单了解Ajax
- 个人学习总结一机器学习入门(二)
- 198. House Robber && 213. House Robber II
- Hibernate_映射_关联关系_一对多多对一映射1
- 多线程通俗演义
- Object类型
- Vs2010与VC的区别
- js 原型莲
- spring实现上传文件
- CABasicAnimation的基本使用方法(移动·旋转·放大·缩小)
- ZOJ3938-Defuse the Bomb
- 剑指offer5:从头到尾打印链表