---
title: "双向关联多对多"
author: "小牛犊"
date: "2008-09-25T11:32:06Z"
tags: ["Hibernate", "SSH", "XML"]
slug: "untitled"
draft: false
lastmod: "2008-09-25T11:32:06Z"
id: 246626
url: "https://xiaoniudu2008.iteye.com/blog/246626"
---

inverse:反转

 

1\) inverse="false" 表示为主控方，主控方负责维护关联关系，一般在一对多关系中，把多的一方设置为inverse="false"。

 

2\) lazy="false表示是延迟加载，当为true时，启动延迟加载，如两个关联a,b不延迟加载时可能加载a时候，也把b加载了，但你可能没用b，只用a了，这就造成了浪费，延迟加载时表示用到b时才加载。

```
学生Student.hbm.xml：
<hibernate-mapping>
    <class name="com.ssh.Student" table="student" schema="dbo" catalog="sshcourse">
        <id name="stuId" type="integer">
            <column name="stu_id" />
            <generator class="native" />
        </id>
        <property name="stuName" type="string">
            <column name="stu_name" length="50" />
        </property>
        <property name="stuPwd" type="string">
            <column name="stu_pwd" length="50" />
        </property>
        <set name="courses"
        table="course_student_table"
        cascade="save-update"
        inverse="false" 
        lazy="false">
        <key column="stu_id"></key>
        <many-to-many class="com.ssh.Course" column="cou_id"></many-to-many>
        </set>
    </class>
</hibernate-mapping>
课程Course.hbm.xml：
<hibernate-mapping>
    <class name="com.ssh.Course" table="course" schema="dbo" catalog="sshcourse">
        <id name="couId" type="integer">
            <column name="cou_id" />
            <generator class="native" />
        </id>
        <property name="couName" type="string">
            <column name="cou_name" length="50" />
        </property>
        <property name="couDis" type="string">
            <column name="cou_dis" length="50" />
        </property>
        <set name="students"
        table="course_student_table"
        cascade="save-update"
        inverse="true">
        <key column="cou_id"></key>
        <many-to-many class="com.ssh.Student" column="stu_id"></many-to-many>
        </set>
    </class>
</hibernate-mapping>
他们之间是通过course_student_table学生课程表关联的
```