1、一对多关联映射 (one–to --many)
(1)主表关联映射的实体类将字表实体类对象作为集合属性进行关联
private Set<字表实体类> 集合属性名称 (主表实体类映射的字表的实体对象集合属性)
映射文件中一对多关联映射
<set name ="集合属性名称" inverse=“true” cascad = "all |save|delete" lazy="true/false">
....
<one-to-many class="包名.字表实体类"/>
</set>
注意
set元素只能在主表中设置!
cascad属性只能在set元素中设置!
指定集合元素的类型
lazy="true"只能设置在set元素节点
2、多对一的关联映射(many–to–one)
(1)子表实体类将主表实体类对象作为关联属性进行映射
private 主表实体类名 主表关联属性对象;
(2)子表实体类的映射文件
<many-to-one name="主表关联属性对象" class="包名.主表实体类名" fetch="select" lazy="false"/>
下面的代码是对员工的基本操作 ,表的设计如下
##创建部门表
CREATE TABLE dep(
depid INT PRIMARY KEY AUTO_INCREMENT,##主键自增
depname VARCHAR(50) NOT NULL
);
##创建福利表
CREATE TABLE welfare(
wid INT PRIMARY KEY AUTO_INCREMENT,
wname VARCHAR(50) NOT NULL
);
##创建员工表,和部门之间是多对一关系,部门的子表
CREATE TABLE emp(
eid INT PRIMARY KEY AUTO_INCREMENT,
ename VARCHAR(50) NOT NULL,
sex VARCHAR(4) DEFAULT '男',
address VARCHAR(100),
birthday DATE,
photo VARCHAR(50),
depid INT NOT NULL,
CONSTRAINT fk_depid FOREIGN KEY (depid) REFERENCES dep(depid)
);
##员工薪资表(与员工一对一关系)
CREATE TABLE salary(
sid INT PRIMARY KEY AUTO_INCREMENT,
eid INT NOT NULL UNIQUE,
emoney FLOAT CHECK (emoney>=2000),
CONSTRAINT fk_saleid FOREIGN KEY (eid) REFERENCES emp(eid)
);
##员工福利关系表(员工与福利表的关系表(多对多关系))
CREATE TABLE empwelfare(
ewid INT PRIMARY KEY AUTO_INCREMENT,
eid INT NOT NULL,
wid INT NOT NULL,
CONSTRAINT fk_eweid FOREIGN KEY (eid) REFERENCES emp(eid),
CONSTRAINT fk_ewwid FOREIGN KEY (wid) REFERENCES welfare(wid)
);
增加一个员工
Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();
try {
Emp emp = new Emp(new Dep(1), "花如意", "女", "西安", new SimpleDateFormat("yyyy-MM-dd").parse("1998-06-22"), null, null, null);
/******设置薪资*****************/
Set<Salary> salaries = new HashSet<Salary>();
Salary salary = new Salary(emp,5600f);
salaries.add(salary); //设置薪资到集合
emp.setSalaries(salaries); //设置薪资集合到员对象
/******************************/
/**************设置员工福利*****************/
Set<Empwelfare> welfares = new HashSet<Empwelfare>();
//获取员工要选择的福利
Welfare welfare1 = (Welfare) session.load(Welfare.class, 1);
Welfare welfare2 = (Welfare) session.load(Welfare.class, 2);
Welfare welfare3 = (Welfare) session.load(Welfare.class, 3);
Welfare welfare4 = (Welfare) session.load(Welfare.class, 4);
//创建员工福利对象 设置员工对象到关系对象表中
Empwelfare empwelfare1 = new Empwelfare(emp, welfare1);
Empwelfare empwelfare2 = new Empwelfare(emp, welfare2);
Empwelfare empwelfare3 = new Empwelfare(emp, welfare3);
Empwelfare empwelfare4 = new Empwelfare(emp, welfare4);
//将员工福利对象设置到集合中
welfares.add(empwelfare1);
welfares.add(empwelfare2);
welfares.add(empwelfare3);
welfares.add(empwelfare4);
//将集合设置到员工对象中
emp.setEmpwelfares(welfares);
/**************************************/
session.save(emp);
tx.commit();
System.out.println("保存成功");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
HibernateSessionFactory.closeSession();
}
修改员工信息
Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();
try {
Emp emp = (Emp)session.load(Emp.class, 12);
emp.setEname("陆小果");
emp.setSex("男");
/******修改薪资*****************/
//获取原来的员工薪资集合
Set<Salary> oldss = emp.getSalaries();
if(oldss!=null &&oldss.size() >0 ){ //判断原来是否存在薪资
for (Salary oldsa : oldss) {
oldsa.setEmoney(9000f);
}
emp.setSalaries(oldss);
}else{ //否则新增薪资
Set<Salary> salaries = new HashSet<Salary>();
Salary salary = new Salary(emp,5600f);
salaries.add(salary); //设置薪资到集合
emp.setSalaries(salaries); //设置薪资集合到员对象
}
/******************************/
/**************修改员工福利*****************/
//获取原来得员工福利集合
Set<Empwelfare> oldws = emp.getEmpwelfares();
if(oldws != null && oldws.size() >0){ //原来的福利是否存在
//删除原来的福利(删除关系表的数据)
for (Empwelfare oldwf : oldws) {
session.delete(oldwf);
}
}
Set<Empwelfare> welfares = new HashSet<Empwelfare>();
//获取员工要选择的福利
Welfare welfare1 = (Welfare) session.load(Welfare.class, 1);
Welfare welfare2 = (Welfare) session.load(Welfare.class, 4);
Welfare welfare3 = (Welfare) session.load(Welfare.class, 5);
Welfare welfare4 = (Welfare) session.load(Welfare.class, 6);
//创建员工福利对象 设置员工对象到关系对象表中
Empwelfare empwelfare1 = new Empwelfare(emp, welfare1);
Empwelfare empwelfare2 = new Empwelfare(emp, welfare2);
Empwelfare empwelfare3 = new Empwelfare(emp, welfare3);
Empwelfare empwelfare4 = new Empwelfare(emp, welfare4);
//将员工福利对象设置到集合中
welfares.add(empwelfare1);
welfares.add(empwelfare2);
welfares.add(empwelfare3);
welfares.add(empwelfare4);
//将集合设置到员工对象中
emp.setEmpwelfares(welfares);
/**************************************/
session.merge(emp);
tx.commit();
System.out.println("更新成功");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
HibernateSessionFactory.closeSession();
}
删除员工
Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();
try {
Emp emp = (Emp)session.load(Emp.class, 13);
session.delete(emp);
tx.commit();
System.out.println("删除成功");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
HibernateSessionFactory.closeSession();
}
查询所有员工信息
Session session = HibernateSessionFactory.getSession();
Query query = session.createQuery("from com.po.Emp");
List<Emp> emps = query.list();
for (Emp emp : emps) {
System.out.println("编号:" + emp.getEid());
System.out.println("姓名:" + emp.getEname());
System.out.println("性别:" + emp.getSex());
System.out.println("地址:" + emp.getAddress());
System.out.println("生日:" + emp.getBirthday());
System.out.println("部门:" + emp.getDep().getDepname());
System.out.println("薪资:" + ((Salary)emp.getSalaries().toArray()[0]).getEmoney());
System.out.println("福利:");
Set<Empwelfare> empwelfares = emp.getEmpwelfares();
for (Empwelfare empwelfare : empwelfares) {
System.out.println(empwelfare.getWelfare().getWname());
}
System.out.println("===================================");
}