1.创建如下项目结构
2.在项目的src下创建hibernate.cfg.xml主配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" > 3 <hibernate-configuration> 4 <session-factory> 5 <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> 6 <property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:orcl</property> 7 <property name="connection.username">scott</property> 8 <property name="connection.password">tiger</property> 9 <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> 10 <property name="show_sql">true</property> 11 <property name="format_sql">true</property> 12 13 <mapping resource="com/entity/Dept.hbm.xml"/> 14 <mapping resource="com/entity/Emp.hbm.xml"/> 15 </session-factory> 16 </hibernate-configuration>
3.在项目的src下的com.entity包下创建Dept.java
1 package com.entity; 2 3 import java.util.HashSet; 4 import java.util.Set; 5 6 public class Dept { 7 /** 8 * 部门编号 9 */ 10 private int deptno; 11 /** 12 * 部门名称 13 */ 14 private String dname; 15 /** 16 * 部门位置 17 */ 18 private String loc; 19 /** 20 * 创建set集合 21 */ 22 private Set<Emp> empset=new HashSet<Emp>(); 23 24 public Dept() { 25 } 26 public Dept(int deptno, String dname, String loc) { 27 this.deptno = deptno; 28 this.dname = dname; 29 this.loc = loc; 30 } 31 32 public Dept(int deptno, String dname, String loc, Set<Emp> empset) { 33 this.deptno = deptno; 34 this.dname = dname; 35 this.loc = loc; 36 this.empset = empset; 37 } 38 public int getDeptno() { 39 return deptno; 40 } 41 public void setDeptno(int deptno) { 42 this.deptno = deptno; 43 } 44 public String getDname() { 45 return dname; 46 } 47 public void setDname(String dname) { 48 this.dname = dname; 49 } 50 public String getLoc() { 51 return loc; 52 } 53 public void setLoc(String loc) { 54 this.loc = loc; 55 } 56 57 public Set<Emp> getEmpset() { 58 return empset; 59 } 60 public void setEmpset(Set<Emp> empset) { 61 this.empset = empset; 62 } 63 @Override 64 public String toString() { 65 return "Dept [deptno=" + deptno + ", dname=" + dname + ", empset=" 66 + empset + ", loc=" + loc + "]"; 67 } 68 69 70 }
4.在项目的src下的com.entity包下创建Emp.java
1 package com.entity; 2 3 import java.util.Date; 4 5 public class Emp { 6 /** 7 * 员工编号 8 */ 9 private Integer empno; 10 /** 11 * 员工姓名 12 */ 13 private String ename; 14 /** 15 * 员工工作 16 */ 17 private String job; 18 /** 19 * 员工领导编号 20 */ 21 private Integer mgr; 22 /** 23 * 员工雇佣日期 24 */ 25 private Date hiredate; 26 /** 27 * 员工工资 28 */ 29 private Integer sal; 30 /** 31 * 员工奖金 32 */ 33 private Integer comm; 34 /** 35 * 部门外键 36 * 引入部门对象 37 */ 38 private Dept dept; 39 public Emp() { 40 super(); 41 } 42 public Emp(Integer empno, String ename, String job, Integer mgr, 43 Date hiredate, Integer sal, Integer comm, Dept dept) { 44 this.empno = empno; 45 this.ename = ename; 46 this.job = job; 47 this.mgr = mgr; 48 this.hiredate = hiredate; 49 this.sal = sal; 50 this.comm = comm; 51 this.dept = dept; 52 } 53 54 public Integer getEmpno() { 55 return empno; 56 } 57 public void setEmpno(Integer empno) { 58 this.empno = empno; 59 } 60 public String getEname() { 61 return ename; 62 } 63 public void setEname(String ename) { 64 this.ename = ename; 65 } 66 public String getJob() { 67 return job; 68 } 69 public void setJob(String job) { 70 this.job = job; 71 } 72 public Integer getMgr() { 73 return mgr; 74 } 75 public void setMgr(Integer mgr) { 76 this.mgr = mgr; 77 } 78 public Date getHiredate() { 79 return hiredate; 80 } 81 public void setHiredate(Date hiredate) { 82 this.hiredate = hiredate; 83 } 84 public Integer getSal() { 85 return sal; 86 } 87 public void setSal(Integer sal) { 88 this.sal = sal; 89 } 90 public Integer getComm() { 91 return comm; 92 } 93 public void setComm(Integer comm) { 94 this.comm = comm; 95 } 96 public Dept getDept() { 97 return dept; 98 } 99 public void setDept(Dept dept) { 100 this.dept = dept; 101 } 102 @Override 103 public String toString() { 104 return "Emp [comm=" + comm + ", dept=" + dept + ", empno=" + empno 105 + ", ename=" + ename + ", hiredate=" + hiredate + ", job=" 106 + job + ", mgr=" + mgr + ", sal=" + sal + "]"; 107 } 108 109 110 111 112 113 114 }
5.在项目的src下的com.entity包下创建Dept.hbm.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > 3 <hibernate-mapping> 4 <class name="com.entity.Dept" table="DEPT" schema="SCOTT" lazy="true"> 5 <!-- 主键配置 --> 6 <id name="deptno" type="java.lang.Integer" column="DEPTNO"> 7 <!-- 主键由应用程序负责生成 --> 8 <generator class="assigned"/> 9 </id> 10 <!-- 部门名称 --> 11 <property name="dname" type="java.lang.String" column="DNAME"/> 12 13 <!-- 部门位置 --> 14 <property name="loc" type="java.lang.String" column="LOC"/> 15 16 <!-- 17 一对多映射 18 cascade级联:操作主表时同时操作从表 19 cascade="save-update" 级联增和该 20 cascade="delete" 级联删除 21 cascade="none" 忽略关联 22 cascade="all"级联增删改 23 --> 24 <!-- inverse="false"主动维护关联关系,false主动方,true被动方(当从表修改时,主表不修改) 25 inverse="false"两条update 26 inverse="true" 一条update --> 27 <set name="empset" cascade="all" inverse="true" order-by="ename asc"> 28 <!-- 本表主键列,对应emp的外键列 --> 29 <key column="DEPTNO"></key> 30 <!-- 集合存储的数据类型 --> 31 <one-to-many class="com.entity.Emp"/> 32 </set> 33 </class> 34 </hibernate-mapping>
6.在项目的src下的com.entity包下创建Emp.hbm.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > 3 <hibernate-mapping> 4 <class name="com.entity.Emp" table="EMP" schema="scott" > 5 <id name="empno" type="java.lang.Integer" column="EMPNO"> 6 <generator class="assigned"/> 7 </id> 8 <property name="ename" type="java.lang.String" column="ENAME"/> 9 <property name="job" type="java.lang.String" column="JOB"/> 10 <property name="mgr" type="java.lang.Integer" column="MGR"/> 11 <property name="hiredate" type="java.util.Date" column="HIREDATE"/> 12 <property name="sal" type="java.lang.Integer" column="SAL"/> 13 <property name="comm" type="java.lang.Integer" column="COMM" not-null="false" /> 14 15 <!-- 多对一映射:name是emp中外键字段引用的对象,class是对象的类型,column是数据库中对应的外键列字段名 --> 16 <many-to-one name="dept" class="com.entity.Dept" column="DEPTNO"/> 17 </class> 18 </hibernate-mapping>
7.在项目的src下的com.util包下创建HibernateUtil.java
HibernateUtil.java
8.在项目的src下的com.dao包下创建EmpDao.java
1 package com.dao; 2 3 import java.text.ParseException; 4 import java.text.SimpleDateFormat; 5 import java.util.Date; 6 import java.util.List; 7 import java.util.Set; 8 import java.util.logging.SimpleFormatter; 9 10 import org.hibernate.Query; 11 import org.hibernate.Session; 12 import org.hibernate.Transaction; 13 14 import com.entity.Dept; 15 import com.entity.Emp; 16 import com.util.HibernateUtil; 17 18 public class DeptDao { 19 20 public static void main(String[] args) { 21 System.out.println("--------------------1.保存:部门对象,插入2条,修改1条------------------"); 22 //saveDept(); 23 System.out.println("----------------2.删除部门数据级联删除员工--------------------------------------"); 24 // deleteDept(4); 25 System.out.println("----------------3.修改员工--------------------------------------"); 26 //updateEmp(2,1); 27 System.out.println("-------------------4.查询某部门下的所有员工,并按姓名排序order-by-------------------------------------"); 28 //getEmpByDeptno(10); 29 System.out.println("-------------------5.查询某部门信息--lazy=false-----------------------------------"); 30 //getDeptByDeptno(10); 31 System.out.println("-------------------6.查询某部门信息--lazy=true-----------------------------------"); 32 saveEmp(10); 33 34 } 35 /** 36 * -6.查询某部门信息--lazy=true懒加载 ,查的时候用load懒加载查询的方式 37 * @param id 38 */ 39 private static void saveEmp(int id) { 40 Session session=HibernateUtil.getSession(); 41 Transaction tx=session.beginTransaction(); 42 43 //查单条load懒加载 44 Dept dept=(Dept) session.load(Dept.class, id); 45 Date date=null; 46 try { 47 date = new SimpleDateFormat("yyyy-MM-dd").parse("2013-3-3"); 48 } catch (ParseException e) { 49 e.printStackTrace(); 50 } 51 Emp emp=new Emp(6, "holly", "教员", 2, date, 2300, 200, dept); 52 53 //添加员工信息 54 session.save(emp); 55 tx.commit(); 56 System.out.println("添加成功"); 57 58 HibernateUtil.closeSession(); 59 60 } 61 /** 62 * 5.查询某部门信息 63 * dept类级别的lazy=false 立即加载 ,查的时候用get立即加载的方式 64 * 1条查询sql 65 * @param i 66 */ 67 private static void getDeptByDeptno(int id) { 68 Session session=HibernateUtil.getSession(); 69 70 //查单条 71 Dept dept=(Dept) session.load(Dept.class, id); 72 73 HibernateUtil.closeSession(); 74 75 } 76 /** 77 * 4.查询某部门下的所有员工,并按姓名排序order-by 78 * @param i 79 */ 80 private static void getEmpByDeptno(int id) { 81 Session session=HibernateUtil.getSession(); 82 83 //查单条 84 Dept dept=(Dept) session.get(Dept.class, id); 85 Set<Emp> emps=dept.getEmpset(); 86 for (Emp emp : emps) { 87 System.out.println(emp.getEname()); 88 } 89 HibernateUtil.closeSession(); 90 91 } 92 /** 93 * 3.修改员工 94 * @param empno 95 * @param deptno 96 */ 97 private static void updateEmp(int empno,int deptno) { 98 Session session=HibernateUtil.getSession(); 99 //2.开启事务 100 Transaction tx=session.beginTransaction(); 101 //查单条 102 Dept dept=(Dept) session.load(Dept.class, deptno); 103 Emp emp=(Emp) session.load(Emp.class, empno); 104 /* 105 * 监测到Emp对象的属性发生变化,所以执行 106 * update 107 scott.EMP 108 set 109 ENAME=?, 110 JOB=?, 111 MGR=?, 112 HIREDATE=?, 113 SAL=?, 114 COMM=?, 115 DEPTNO=? 116 where 117 EMPNO=? 118 */ 119 emp.setDept(dept); 120 /*(inverse=false)主动,监测到Dept对象的属性发生变化,所以执行 121 update 122 scott.EMP 123 set 124 DEPTNO=? 125 where 126 EMPNO=? 127 (inverse=true)被动,没有上面一条update 128 */ 129 dept.getEmpset().add(emp); 130 System.out.println("修改成功"); 131 132 tx.commit(); 133 HibernateUtil.closeSession(); 134 135 } 136 137 /** 138 * 2.删除 139 * @param id 140 */ 141 private static void deleteDept(int id) { 142 Session session=HibernateUtil.getSession(); 143 //2.开启事务 144 Transaction tx=session.beginTransaction(); 145 //查单条 146 Dept dept=(Dept) session.get(Dept.class, id); 147 if(dept!=null){ 148 session.delete(dept); 149 System.out.println("删除成功"); 150 }else{ 151 System.out.println("该条数据不存在"); 152 } 153 tx.commit(); 154 HibernateUtil.closeSession(); 155 156 } 157 158 /** 159 * 1.保存:1部门和2员工-----3条sql 160 */ 161 private static void saveDept() { 162 //1.开启Session 163 Session session=HibernateUtil.getSession(); 164 165 //2.开启事务 166 Transaction tx=session.beginTransaction(); 167 168 //3.执行操作 169 //创建部门对象 170 Dept dept=new Dept(3, "教职部", "安德门"); 171 Date date=null; 172 try { 173 date = new SimpleDateFormat("yyyy-MM-dd").parse("2013-3-3"); 174 } catch (ParseException e) { 175 e.printStackTrace(); 176 } 177 178 //创建员工对象 179 Emp emp1=new Emp(4, "holly", "教员", 2, date, 2300, 200, dept); 180 181 //向集合总添加员工信息(1条sql) 182 dept.getEmpset().add(emp1); 183 184 //保存Dept对象(2条插入) 185 session.save(dept); 186 187 //4.提交事务 188 tx.commit(); 189 System.out.println("Emp保存成功"); 190 191 //5.关闭Session 192 HibernateUtil.closeSession(); 193 } 194 195 }
9.结果自己验证...