1.创建如下数据库脚本
1 --1.1 项目表 2 create table PROJECT 3 ( 4 proid NUMBER(6) not null, 5 proname VARCHAR2(50) 6 ) 7 ; 8 --1.2 项目表主键 9 alter table PROJECT 10 add constraint PK_PROJECT_PROID primary key (PROID); 11 12 13 --2.1 员工表 14 create table EMPLOYEE 15 ( 16 empid NUMBER(6) not null, 17 empname VARCHAR2(50) 18 ) 19 ; 20 21 --2.2 员工表主键 22 alter table EMPLOYEE 23 add constraint PK_EMPLOYEE_EMPID primary key (EMPID); 24 25 --3.1中间表 26 create table PROEMP 27 ( 28 rproid NUMBER(6) not null, 29 rempid NUMBER(6) not null 30 ) 31 ; 32 --3.2 中间表主外健 33 alter table PROEMP 34 add constraint PK_PROEMP primary key (RPROID, REMPID); 35 36 alter table PROEMP 37 add constraint FK_PROEMP_REMPID foreign key (REMPID) 38 references EMPLOYEE (EMPID); 39 40 alter table PROEMP 41 add constraint FK_PROEMP_RPROID foreign key (RPROID) 42 references PROJECT (PROID);
2.创建javaweb项目Hibernate_Part4_C2
3. 在项目的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 <!-- 数据库就的url地址 --> 6 <property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:orcl</property> 7 8 <property name="dialect"> org.hibernate.dialect.Oracle10gDialect</property> 9 10 <!-- 数据库驱动 --> 11 <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> 12 13 <!-- 数据库用户名 --> 14 <property name="connection.username">scott</property> 15 16 <!-- 数据库密码 --> 17 <property name="connection.password">tiger</property> 18 19 20 <!-- 显示sql语句 --> 21 <property name="show_sql">true</property> 22 23 <!-- sql语句格式化 --> 24 <property name="format_sql">true</property> 25 26 <!-- 注册映射文件 --> 27 <mapping resource="com/entity/Project.hbm.xml"/> 28 <mapping resource="com/entity/Employee.hbm.xml"/> 29 </session-factory> 30 31 </hibernate-configuration>
4.在项目的src下的com.entity包下创建Employee.java类
1 package com.entity; 2 3 import java.util.HashSet; 4 import java.util.Set; 5 6 public class Employee { 7 private Integer empid; 8 private String empname; 9 private Set<Project> projects=new HashSet<Project>(); 10 11 public Employee() { 12 } 13 14 public Employee(Integer empid, String empname) { 15 this.empid = empid; 16 this.empname = empname; 17 } 18 19 20 public Employee(Integer empid, String empname, Set<Project> projects) { 21 this.empid = empid; 22 this.empname = empname; 23 this.projects = projects; 24 } 25 26 public Integer getEmpid() { 27 return empid; 28 } 29 30 public void setEmpid(Integer empid) { 31 this.empid = empid; 32 } 33 34 public String getEmpname() { 35 return empname; 36 } 37 38 public void setEmpname(String empname) { 39 this.empname = empname; 40 } 41 42 public Set<Project> getProjects() { 43 return projects; 44 } 45 46 public void setProjects(Set<Project> projects) { 47 this.projects = projects; 48 } 49 50 @Override 51 public String toString() { 52 return "Employee [empid=" + empid + ", empname=" + empname 53 + ", projects=" + projects + "]"; 54 } 55 56 57 }
5.在项目的src下的com.entity包下创建Employee.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 <!-- 持久化类的映射文件 --> 5 <class name="com.entity.Employee" table="EMPLOYEE"> 6 <!-- 主键映射 --> 7 <id name="empid" type="java.lang.Integer" column="empid"> 8 <!-- 主键插入由应用程序负责生成 --> 9 <generator class="assigned"/> 10 </id> 11 <!-- 普通字段映射 --> 12 <!-- 员工名称称映射 --> 13 <property name="empname" type="java.lang.String" column="EMPNAME"/> 14 15 <!-- 多对多 中间表字段--> 16 <set name="projects" table="PROEMP" inverse="true"> 17 <!-- 中间表和员工表关联的外键字段 --> 18 <key column="REMPID"></key> 19 20 <!-- 员工中的项目集合 --> 21 <many-to-many class="com.entity.Project" column="RPROID"/> 22 </set> 23 </class> 24 </hibernate-mapping>
6.在项目的src下的com.entity包下创建Project.java类
1 package com.entity; 2 3 import java.util.HashSet; 4 import java.util.Set; 5 6 public class Project { 7 private Integer proid; 8 private String proname; 9 private Set<Employee> employees=new HashSet<Employee>(); 10 11 12 public Project() { 13 } 14 public Project(Integer proid, String proname) { 15 this.proid = proid; 16 this.proname = proname; 17 } 18 19 public Project(Integer proid, String proname, Set<Employee> employees) { 20 this.proid = proid; 21 this.proname = proname; 22 this.employees = employees; 23 } 24 public Integer getProid() { 25 return proid; 26 } 27 public void setProid(Integer proid) { 28 this.proid = proid; 29 } 30 public String getProname() { 31 return proname; 32 } 33 public void setProname(String proname) { 34 this.proname = proname; 35 } 36 37 public Set<Employee> getEmployees() { 38 return employees; 39 } 40 public void setEmployees(Set<Employee> employees) { 41 this.employees = employees; 42 } 43 @Override 44 public String toString() { 45 return "Project [employees=" + employees + ", proid=" + proid 46 + ", proname=" + proname + "]"; 47 } 48 49 50 51 }
7.在项目的src下的com.entity包下创建Project.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 <!-- 持久化类的映射文件 --> 5 <class name="com.entity.Project" table="PROJECT"> 6 <!-- 主键映射 --> 7 <id name="proid" type="java.lang.Integer" column="PROID"> 8 <!-- 主键插入由应用程序负责生成 --> 9 <generator class="assigned"/> 10 </id> 11 <!-- 普通字段映射 --> 12 <!-- 项目名称称映射 --> 13 <property name="proname" type="java.lang.String" column="PRONAME"/> 14 <set name="employees" table="PROEMP" cascade="save-update"> 15 <!-- 中间表和项目表关联的外键字段 --> 16 <key column="RPROID"></key> 17 <many-to-many class="com.entity.Employee" column="REMPID"/> 18 </set> 19 </class> 20 </hibernate-mapping>
8.在项目的src下的com.util包下创建HibernateUtil.java类
1 package com.util; 2 3 import org.hibernate.HibernateException; 4 import org.hibernate.Session; 5 import org.hibernate.SessionFactory; 6 import org.hibernate.cfg.Configuration; 7 8 public class HibernateUtil { 9 /* 10 * 1.创建用线程管理session的对象 还要导入org.hibernate.Session资源 11 */ 12 private static final ThreadLocal<Session> threadLoad = new ThreadLocal<Session>(); 13 14 /* 15 * 2.定义读取配置文件的对象 16 */ 17 private static Configuration configuration = null; 18 19 /* 20 * 3.定义SessionFacotry工厂对象 21 */ 22 private static SessionFactory sessionFactory = null; 23 24 /* 25 * 4.读取配置文件 26 */ 27 static { 28 try { 29 // 读取配置文件 30 configuration = new Configuration().configure(); 31 32 // 创建session工厂对象 33 sessionFactory = configuration.buildSessionFactory(); 34 } catch (HibernateException e) { 35 System.out.println("创建sessionFactory工厂对象错误"); 36 e.printStackTrace(); 37 } 38 } 39 40 /** 41 * 5.获取Session对象 42 * @return session对象 43 */ 44 public static Session getSession() { 45 // 获取当前线程内对应的session对象,可以保证每个线程都有自己的session独享 46 Session session = threadLoad.get(); 47 48 // 如果获取的session为null则新打开session 49 if (session == null) { 50 // 打开一个新的Session 51 session = sessionFactory.openSession(); 52 53 // 将session对象保存到线程中 54 threadLoad.set(session); 55 } 56 // 返回从线程中获取的session对象 57 return session; 58 } 59 60 /** 61 * 6.关闭session对象 62 */ 63 public static void closeSession(){ 64 //获取当前线程内对应的session对象,可以保证每个线程对象都有自己的session对象 65 Session session=threadLoad.get(); 66 67 //将线程中的session置空 68 threadLoad.set(null); 69 70 //如果session不为空,则关闭session 71 if(session!=null){ 72 //关闭session 73 session.close(); 74 } 75 76 77 } 78 79 }
9.在项目的src下的com.dao包下创建ProEmpDao.java类
1 package com.dao; 2 3 import org.hibernate.Session; 4 import org.hibernate.Transaction; 5 6 import com.entity.Employee; 7 import com.entity.Project; 8 import com.util.HibernateUtil; 9 10 public class ProEmpDao { 11 public static void main(String[] args) { 12 //开启session 13 Session session=HibernateUtil.getSession(); 14 //开启事务 15 Transaction tx=session.beginTransaction(); 16 //创建多个员工 17 Employee emp1=new Employee(1, "张三"); 18 Employee emp2=new Employee(2, "李四"); 19 20 //创建项目存在 21 Project p1=new Project(1, "1号项目"); 22 Project p2=new Project(2, "2号项目"); 23 24 //多个人负责一个项目 25 p1.getEmployees().add(emp1); 26 p1.getEmployees().add(emp2); 27 28 //一个项目多个人负责 29 emp1.getProjects().add(p1); 30 emp2.getProjects().add(p1); 31 32 //一个人负责多个项目 33 p2.getEmployees().add(emp1); 34 35 //一个项目一个人负责 36 emp1.getProjects().add(p2); 37 38 //保存项目时级联保存人信息 39 session.save(p1); 40 session.save(p2); 41 42 tx.commit(); 43 HibernateUtil.closeSession(); 44 45 46 } 47 48 }