第一种:一对一的关系

 假设有2个表:person 和idcard

  在person和idcard表的pojo 类里面分别设置另一个表的属性:private Person person;(Idcard类里面声明person类) private Idcard idcard (Person类里面声明)

 

在配置文件中为如下表示:主表person:  <one-to-one name="idcard" class="com.guoc.vo.Idcard" cascade="all">
       
        </one-to-one>

 副表idcard:副表的id根据外键即主表的id<generator class="foreign">

<param name="property">person</param>

            </generator><one-to-one name="person" class="com.guoc.vo.Person"

         constrained="true">
        </one-to-one>

 

在操作的时候:

Person person = new Person();

  person.setPid(idcardForm.getString("pid"));ic.setPerson(person);

第二种:一对多:

假设有2个表 部门dept(主表)和人员emp(从表)

在主表的pojo类里面声明:private Set emps;

在从表的pojo类里面声明:private Dept dept;

 

(主表)配置文件中的写法为:

<set name="emps" table="emp" inverse="true" cascade="all">

            <key>

                <column name="deptno" length="32" />

            </key>

            <one-to-many class="com.guoc.vo.Emp" />

        </set>

 

(从表)配置文件中的写法为:

<many-to-one name="dept" class="com.guoc.vo.Dept">

            <column name="deptno" length="32" />

        </many-to-one>

操作的时候为:

Dept dept=new Dept();

  dept.setDeptno(empForm.getDeptno());

  emp.setDept(dept);

第三中:多对多

假设有2个表:student 和course 有多对多的关系,那必须得有另一个中间表(student_course)存储他们之间的对应关系

这样的话 就转为2个一对多的关系了

student_course的表结构为:

 

create table student_course(
   cid int not null,

   sid int not null,

   

  foreign key (sid) references student(sid),

  

  foreign key (cid) references course(cid));

 

在student 和course的pojo 类里面都得为对应类声明,分别为:

private Set coures;
private Set students;

 

在student的配置文件中表示为:<set name="coures" inverse="true" cascade="all" table="student_course">
     

<key>

                <column name="sid" not-null="true" />

            </key>

            <many-to-many class="com.guoc.vo.Course" column="cid"/>

        </set>

在course的配置文件中表示为:<set name="students" cascade="all" inverse="true" table="student_course">
  

<key>

                <column name="cid" not-null="true" />

            </key>

            <many-to-many class="com.guoc.vo.Student" column="sid"/>

        </set>

这样写好后,虽然能对student里的courses集合进行操作,但是集合里的数据一直为空,因为他们还没有关联数据,student_course表是空的,所以就得有一个操作把关联数据插入到student_course里面

所以得在pojo类里面为student_course设置一个集合属性:

private Set sids;
 private Set cids;

这样student就可以通过cids管理他和course表的对应关系

course就可以通过sids管理他和student表的对应关系

管理是建立在student_course表上的

 

student配置文件再加上此类语句:

<set name="cids" table="student_course" cascade="all">

        <key>

        <column name="sid"></column>

        </key>

        <element type="java.lang.Integer" column="cid"></element>

        </set>

course配置文件加上此类语句:

<set name="sids" cascade="all" table="student_course">

        <key>

         <column name="cid"></column>

        </key>

        <element type="java.lang.Integer" column="sid"></element>

        </set>

相当student和course 表在student_course表里 是一对多的关系

student.getCids().add(new Integer(str[i]));

   DAOFactory.getStudentDAOInstance().update(student);

 取消关联的时候为:

Course c = DAOFactory.getCourseDAOInstance().selectById(cid) ;

   c.getSids().remove(new Integer(sid)) ;

   DAOFactory.getCourseDAOInstance().update(c) ;