多对一关系,,如多个员工对应一个部门,在查员工的时候很想知道他是在哪个部门的,多表查询
方法:
在创建完两个表后要设一个为外键加到另一个里面(建数据表的时候)
注意:级联删除数据,关联表设定的一定是id 设其他属性引起的关联没有用!!
而且级联操作的两个必要条件是:1.测试方法中一定要有两个类相互关联的体现:emp.setdep(dep), 2.在多的那表的映射文件中的many-to-one中有cascade="all"
①在多的一方的java类中加入一的一方的对象如private dept dept;//通过new dept 的对象,对dep.set....,再emp.setdep(dept);,这样两者就建立连接了!
②在多的一方的java映射文件中的属性声明下面写 <many-to-one name="dept" column="deptno" class="dept" cascade="all"></many-to-one>
对应class中的声明对象 对应外键 对应class 如果要级联炒作的属性
dept.java:
package com.hibernate.entity;
public class dept {
public dept(Integer deptno, String depname) {
super();
this.deptno = deptno;
this.depname = depname;
}
public dept() {
super();
// TODO Auto-generated constructor stub
}
private Integer deptno;
private String depname;
public Integer getDeptno() {
return deptno;
}
public void setDeptno(Integer deptno) {
this.deptno = deptno;
}
public String getDepname() {
return depname;
}
public void setDepname(String depname) {
this.depname = depname;
}
}
dept.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.hibernate.entity">
<class name="dept" table="dept">
<id name="deptno" column="deptno">
<generator class="increment"></generator>
</id>
<property name="depname" column="depname"></property>
</class>
</hibernate-mapping>
----------------------
employee.java:
package com.hibernate.entity;
public class employee {
public employee() {
super();
}
private Integer eid;
private String empname;
private Integer salary;
private dept dept;
public Integer getEid() {
return eid;
}
public void setEid(Integer eid) {
this.eid = eid;
}
public String getEmpname() {
return empname;
}
public void setEmpname(String empname) {
this.empname = empname;
}
public Integer getSalary() {
return salary;
}
public void setSalary(Integer salary) {
this.salary = salary;
}
public dept getDept() {
return dept;
}
public void setDept(dept dept) {
this.dept = dept;
}
public employee(Integer eid, String empname, Integer salary, com.hibernate.entity.dept dept) {
super();
this.eid = eid;
this.empname = empname;
this.salary = salary;
this.dept = dept;
}
}
--------------------------------------------
employee.hbm.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.hibernate.entity">
<class name="employee" table="employee">
<id name="eid" column="eid">
<generator class="increment"></generator>
</id>
<property name="empname" column="empname"></property>
<property name="salary" column="salary"></property>
<many-to-one name="dept" column="deptno" class="dept" cascade="all"></many-to-one>
</class>
</hibernate-mapping>
-----------------------------------------------------
测试类
package com.hibernate.test;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.hibernate.entity.dept;
import com.hibernate.entity.employee;
public class many2one {
public static void main(String[] args) {
//查询
employee emp=selectAll(1);
System.out.println(emp.getEmpname());
System.out.println(emp.getSalary());
System.out.println(emp.getDept().getDepname());
System.out.println(emp.getDept().getDeptno());
//添加成员,多联
addemp();
//删除,多联
delete(5);
}
public static employee selectAll(Integer id)
{
employee emp=null;
Session session=Util.getsession();
emp=(employee) session.get(employee.class, id);
Hibernate.initialize(emp.getDept());
session.close();
return emp;
}
public static void addemp()
{
Session session=Util.getsession();
Transaction tx=session.beginTransaction();
employee emp=new employee();
emp.setEmpname("dali");
emp.setSalary(5500);
dept dep=new dept();
dep.setDepname("houqing");
emp.setDept(dep);
session.save(emp);
tx.commit();
session.close();
}
public static void delete(Integer id)
{
Session session=Util.getsession();
Transaction tx=session.beginTransaction();
employee emp=new employee();
emp.setEid(id);
dept dep=new dept();
dep.setDeptno(4);
emp.setDept(dep);
session.delete(emp);
tx.commit();
session.close();
}
}