现在的位置: 首页 > 综合 > 正文

Hibernate的关联映射

2013年11月14日 ⁄ 综合 ⁄ 共 3455字 ⁄ 字号 评论关闭

员工表和部门表就是多对一的关系

  部门表和员工表就是一对多的关系

  员工表Emp表

 public class Emp
{
  private int empId;
  private String empName;
  private Dept dept;       //员工对应的部门使用Dept类型的对象保存
  //省略了get和set方法
}

部门表 Dept 表

public class Dept
{
    private int deptid;
    private String deptname;
private Set<Emp> emps;   //部门下的员工集合使用set保存
//省略了get和set方法
}

1.     多对一映射

 员工表和部门表就是多对一的关系

员工类的xml配置文件 Emp.hbm.xml

<hibernate-mapping package="entity">
    <class name="Emp" table="emp2">
        <id name="empId">
            <generator class="native"/>
        </id>
        <property name="empName"/>
        <!--many-to-one中设置了dept非空,所以在session保持的时候需要按照顺序来,先添加部门,再添加员工,
               如果可为空的话,顺序反过来的话,添加员工后其实员工数据中的部门id信息是为空的,在添加了部门信息以后,
           hibernate还会再update员工的信息来保存部门的信息,不为空就自然要求顺序了-->
        <!-- many-to-one中的name指的是Emp类中的dept属性,column=dept_id为数据库中的列名,hibernate
        识别到dept的类型为Dept后,自动将Dept所对应的表的主键作为Emp对应表中dept_id的外键 -->
        <!--lazy是否执行懒加载,在多对一中,默认为true在查询的时候会在需要用的时候查询,但是往往session
        会在查询后直接关闭,想再获取emp中的dept就获取不到了,提示no session错误,设置lazy为false就不会懒加载了-->
        <!-- fetch设置级联查询,查询语句使用级联查询 -->  <!-- 如果没有指定多对一的column属性,则自动将Dept中的主键作为外键在Emp中创建出来 -->
        <many-to-one name="dept"column="dept_id" not-null="false"lazy="false" fetch="join"></many-to-one>
    </class>
</hibernate-mapping>

操作:

//多对一的添加,HibernaUtil为工具类

Sessionsession =HibernateUtil.getSession();
        Transaction tran=session.beginTransaction();
        Dept dept=new Dept();
        dept.setDeptname("开发部");
        Emp emp =new Emp();
        emp.setEmpName("汉汉");
        emp.setDept(dept);  
        session.save(dept);    //先添加部门,再添加员工,可以提高效率
        session.save(emp);
        tran.commit();
        session.close();
        */
        //多对一查询
    /*
    Sessionsession =HibernateUtil.getSession();
     Empemp=(Emp)session.get(Emp.class,46);
     System.err.println("员工姓名:"+emp.getEmpName());
    session.close();
    //如果没有设置lazy=false的话在session关闭后是取不到部门信息的
    System.err.println("员工部门:"+emp.getDept().getDeptname());
        */

2.一对多映射

  部门表和员工表就是一对多的关系

 部门类的xml的配置文件Dept.hbm.xml

<hibernate-mapping package="entity">
    <class name="Dept" table="dept2">
       <id name="deptid">
            <generator class="native"/>
       </id>
       <property name="deptname"/>
          //Dept中的emps在xml中使用set标签
       <set name="emps" lazy="false" cascade="all">//cascade级联操作
         <key column="dept_id"/>      //指定副表的外键  <!-- 如果没有指明key属性,则会自动将部门的主键作为Emp中的外键创建出来-->
         <one-to-many class="Emp"/>  //指定副表的类
       </set>
    </class>
</hibernate-mapping>

//一对多的添加

Sessionsession=HibernateUtil.getSession();
    Transactiontran=session.beginTransaction();
    Deptdept=new Dept();
    dept.setDeptname("研究部2");
    Set<Emp>set=new HashSet<Emp>();
   
    Empemp1=new Emp();
    emp1.setEmpName("哈哈2");
    emp1.setDept(dept);
    set.add(emp1);
   
    Empemp2=new Emp();
    emp2.setEmpName("呵呵2");
    emp2.setDept(dept);
    set.add(emp2);
     dept.setEmps(set); 
//设置了级联后添加了部门,员工也自然添加了,不过这只是个例子,真实的业务中应该很少有添加部门后立马添加员工的操作
    session.save(dept);
    tran.commit();
    session.close();
   
   
    //一对多的查询
    Sessionsession=HibernateUtil.getSession();
    Dept dept=(Dept)session.get(Dept.class,44);
    Set<Emp>empset=dept.getEmps();
    for(Emp e:empset)
    {
         System.out.println("员工:"+e.getEmpName()+","+"部门:"+e.getDept().getDeptname());
    }
    session.close();

3.一对一映射

身份证和人就是一对一的关系

人实体类 Person类:

  public class Person {
           private int pid;
           private String name;
           private IdCard idcard;
            //省略get和set方法
    }

身份证实体类 IdCard类:

  public class IdCard {
           private int cid;
           private String fromCity;
        private Person person;
          //省略get和set方法
}

   人实体类对应的xml文件  Person.hbm.xml(主表的xml配置)

  <hibernate-mappingpackage="entity">
    <class name="Person"table="person">
       <id name="pid">
            <generatorclass="native"/>
       </id>
       <property name="name"/>
       <one-to-one name="idcard"/> 
        </class>
</hibernate-mapping>

身份证实体类的xml文件 IdCard.hbm.xml(从表的xml配置)

<hibernate-mappingpackage="entity">
  <classname="IdCard" table="id_card">
         <idname="cid">
            <generatorclass="foreign">   //外键设置
              <paramname="property">person</param>
            </generator>
         </id>
         <propertyname="fromCity"/>
<one-to-one name="person"constrained="true"/>    //constrained=”true”:添加约束,实现外键
  </class>
</hibernate-mapping>

抱歉!评论已关闭.