Hibernate 一对多映射文件配置

说明:一个部门对应多个员工,部门中有个员工的集合,并指定集合的名称为数据库表的名称,部门与多个员工的对象是关联的,通过员工的departmentId 与 部门对象的id进行关联

department 映射文件配置(一)

<!DOCTYPE hibernate-mapping PUBLIC

        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="cn.itcast.h_hbm_oneToMany">

    <class name="Department" table="department">

       <id name="id">

           <generator class="native"></generator>

       </id>

       <property name="name"></property>

       <!-- employees属性,Set集合,表达的是Department与Employee的一对多

           inverse属性:true表示自己是否放弃维护关联关系,默认为false。

        -->

       <set name="employees" inverse="true">

           <key column="departmentId"></key>

           <one-to-many class="Employee"/>

       </set>

    </class>

</hibernate-mapping>

 

说明:多对一,指定多的部门唯一的列即可对应查出Department的所有信息

 employees 映射文件配置(多)

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="cn.itcast.h_hbm_oneToMany">

    <class name="Employee" table="employee">

       <id name="id">

           <generator class="native"></generator>

       </id>

       <property name="name"></property>

       <!-- department属性,Department类型,表示Employee与Department的多对一 -->

       <many-to-one name="department" column="departmentId" class="Department"></many-to-one>

    </class>

</hibernate-mapping>

 

posted @ 2013-03-29 16:28  oftenlin  阅读(5538)  评论(0编辑  收藏  举报