1.创建如下数据库脚本
--1.1 项目表
create table PROJECT
(
proid NUMBER(6) not null,
proname VARCHAR2(50)
)
;
--1.2 项目表主键
alter table PROJECT
add constraint PK_PROJECT_PROID primary key (PROID); --2.1 员工表
create table EMPLOYEE
(
empid NUMBER(6) not null,
empname VARCHAR2(50)
)
; --2.2 员工表主键
alter table EMPLOYEE
add constraint PK_EMPLOYEE_EMPID primary key (EMPID); --3.1中间表
create table PROEMP
(
rproid NUMBER(6) not null,
rempid NUMBER(6) not null
)
;
--3.2 中间表主外健
alter table PROEMP
add constraint PK_PROEMP primary key (RPROID, REMPID); alter table PROEMP
add constraint FK_PROEMP_REMPID foreign key (REMPID)
references EMPLOYEE (EMPID); alter table PROEMP
add constraint FK_PROEMP_RPROID foreign key (RPROID)
references PROJECT (PROID);
2.创建javaweb项目Hibernate_Part4_C2
3. 在项目的src下创建hibernate.cfg.xml的主配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
<hibernate-configuration>
<session-factory>
<!-- 数据库就的url地址 -->
<property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:orcl</property> <property name="dialect"> org.hibernate.dialect.Oracle10gDialect</property> <!-- 数据库驱动 -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <!-- 数据库用户名 -->
<property name="connection.username">scott</property> <!-- 数据库密码 -->
<property name="connection.password">tiger</property> <!-- 显示sql语句 -->
<property name="show_sql">true</property> <!-- sql语句格式化 -->
<property name="format_sql">true</property> <!-- 注册映射文件 -->
<mapping resource="com/entity/Project.hbm.xml"/>
<mapping resource="com/entity/Employee.hbm.xml"/>
</session-factory> </hibernate-configuration>
hibernate.cfg.xml
4.在项目的src下的com.entity包下创建Employee.java类
package com.entity; import java.util.HashSet;
import java.util.Set; public class Employee {
private Integer empid;
private String empname;
private Set<Project> projects=new HashSet<Project>(); public Employee() {
} public Employee(Integer empid, String empname) {
this.empid = empid;
this.empname = empname;
} public Employee(Integer empid, String empname, Set<Project> projects) {
this.empid = empid;
this.empname = empname;
this.projects = projects;
} public Integer getEmpid() {
return empid;
} public void setEmpid(Integer empid) {
this.empid = empid;
} public String getEmpname() {
return empname;
} public void setEmpname(String empname) {
this.empname = empname;
} public Set<Project> getProjects() {
return projects;
} public void setProjects(Set<Project> projects) {
this.projects = projects;
} @Override
public String toString() {
return "Employee [empid=" + empid + ", empname=" + empname
+ ", projects=" + projects + "]";
} }
Employee.java
5.在项目的src下的com.entity包下创建Employee.hbm.xml映射文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<!-- 持久化类的映射文件 -->
<class name="com.entity.Employee" table="EMPLOYEE">
<!-- 主键映射 -->
<id name="empid" type="java.lang.Integer" column="empid">
<!-- 主键插入由应用程序负责生成 -->
<generator class="assigned"/>
</id>
<!-- 普通字段映射 -->
<!-- 员工名称称映射 -->
<property name="empname" type="java.lang.String" column="EMPNAME"/> <!-- 多对多 中间表字段-->
<set name="projects" table="PROEMP" inverse="true">
<!-- 中间表和员工表关联的外键字段 -->
<key column="REMPID"></key> <!-- 员工中的项目集合 -->
<many-to-many class="com.entity.Project" column="RPROID"/>
</set>
</class>
</hibernate-mapping>
Employee.hbm.xml
6.在项目的src下的com.entity包下创建Project.java类
package com.entity; import java.util.HashSet;
import java.util.Set; public class Project {
private Integer proid;
private String proname;
private Set<Employee> employees=new HashSet<Employee>(); public Project() {
}
public Project(Integer proid, String proname) {
this.proid = proid;
this.proname = proname;
} public Project(Integer proid, String proname, Set<Employee> employees) {
this.proid = proid;
this.proname = proname;
this.employees = employees;
}
public Integer getProid() {
return proid;
}
public void setProid(Integer proid) {
this.proid = proid;
}
public String getProname() {
return proname;
}
public void setProname(String proname) {
this.proname = proname;
} public Set<Employee> getEmployees() {
return employees;
}
public void setEmployees(Set<Employee> employees) {
this.employees = employees;
}
@Override
public String toString() {
return "Project [employees=" + employees + ", proid=" + proid
+ ", proname=" + proname + "]";
} }
Project.java
7.在项目的src下的com.entity包下创建Project.hbm.xml映射文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<!-- 持久化类的映射文件 -->
<class name="com.entity.Project" table="PROJECT">
<!-- 主键映射 -->
<id name="proid" type="java.lang.Integer" column="PROID">
<!-- 主键插入由应用程序负责生成 -->
<generator class="assigned"/>
</id>
<!-- 普通字段映射 -->
<!-- 项目名称称映射 -->
<property name="proname" type="java.lang.String" column="PRONAME"/>
<set name="employees" table="PROEMP" cascade="save-update">
<!-- 中间表和项目表关联的外键字段 -->
<key column="RPROID"></key>
<many-to-many class="com.entity.Employee" column="REMPID"/>
</set>
</class>
</hibernate-mapping>
Project.hbm.xml
8.在项目的src下的com.util包下创建HibernateUtil.java类
package com.util; import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration; public class HibernateUtil {
/*
* 1.创建用线程管理session的对象 还要导入org.hibernate.Session资源
*/
private static final ThreadLocal<Session> threadLoad = new ThreadLocal<Session>(); /*
* 2.定义读取配置文件的对象
*/
private static Configuration configuration = null; /*
* 3.定义SessionFacotry工厂对象
*/
private static SessionFactory sessionFactory = null; /*
* 4.读取配置文件
*/
static {
try {
// 读取配置文件
configuration = new Configuration().configure(); // 创建session工厂对象
sessionFactory = configuration.buildSessionFactory();
} catch (HibernateException e) {
System.out.println("创建sessionFactory工厂对象错误");
e.printStackTrace();
}
} /**
* 5.获取Session对象
* @return session对象
*/
public static Session getSession() {
// 获取当前线程内对应的session对象,可以保证每个线程都有自己的session独享
Session session = threadLoad.get(); // 如果获取的session为null则新打开session
if (session == null) {
// 打开一个新的Session
session = sessionFactory.openSession(); // 将session对象保存到线程中
threadLoad.set(session);
}
// 返回从线程中获取的session对象
return session;
} /**
* 6.关闭session对象
*/
public static void closeSession(){
//获取当前线程内对应的session对象,可以保证每个线程对象都有自己的session对象
Session session=threadLoad.get(); //将线程中的session置空
threadLoad.set(null); //如果session不为空,则关闭session
if(session!=null){
//关闭session
session.close();
} } }
HibernateUtil.java
9.在项目的src下的com.dao包下创建ProEmpDao.java类
package com.dao; import org.hibernate.Session;
import org.hibernate.Transaction; import com.entity.Employee;
import com.entity.Project;
import com.util.HibernateUtil; public class ProEmpDao {
public static void main(String[] args) {
//开启session
Session session=HibernateUtil.getSession();
//开启事务
Transaction tx=session.beginTransaction();
//创建多个员工
Employee emp1=new Employee(1, "张三");
Employee emp2=new Employee(2, "李四"); //创建项目存在
Project p1=new Project(1, "1号项目");
Project p2=new Project(2, "2号项目"); //多个人负责一个项目
p1.getEmployees().add(emp1);
p1.getEmployees().add(emp2); //一个项目多个人负责
emp1.getProjects().add(p1);
emp2.getProjects().add(p1); //一个人负责多个项目
p2.getEmployees().add(emp1); //一个项目一个人负责
emp1.getProjects().add(p2); //保存项目时级联保存人信息
session.save(p1);
session.save(p2); tx.commit();
HibernateUtil.closeSession(); } }
ProEmpDao.java