Hibernate  一对多连接表单向关联
 
 
一、模型介绍
 
一个人(Person)对应多个地址(Address),比如家庭地址、公司地址。
 
二、实体(省略gettersetter方法)
 
public class Person1ntab {
    private int personid;
    private String name;
    private int age;
    private Set addresses=new HashSet();
 
public class Address1nfk implements Serializable {
    private int addressid;
    private String addressdetail;
 
三、表模型
 
mysql> desc join_1ntab;
+-----------+---------+------+-----+---------+-------+
| Field     | Type    | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+-------+
| personid  | int(11) | NO   | PRI |         |       |
| addressid | int(11) | NO   | PRI |         |       |
+-----------+---------+------+-----+---------+-------+
 
mysql> desc person_1ntab;
+----------+--------------+------+-----+---------+----------------+
| Field    | Type         | Null | Key | Default | Extra          |
+----------+--------------+------+-----+---------+----------------+
| personid | int(11)      | NO   | PRI | NULL    | auto_increment |
| name     | varchar(255) | YES  |     | NULL    |                |
| age      | int(11)      | YES  |     | NULL    |                |
+----------+--------------+------+-----+---------+----------------+
 
mysql> desc address_1ntab;
+---------------+--------------+------+-----+---------+----------------+
| Field         | Type         | Null | Key | Default | Extra          |
+---------------+--------------+------+-----+---------+----------------+
| addressid     | int(11)      | NO   | PRI | NULL    | auto_increment |
| addressdetail | varchar(255) | YES  |     | NULL    |                |
+---------------+--------------+------+-----+---------+----------------+
 
四、生成的SQL脚本
 
/* Formatted on 2007/08/21 10:58 (QP5 v5.50) */
CREATE TABLE `address_1ntab` (
  `addressid` int(11) NOT NULL auto_increment,
  `addressdetail` varchar(255) default NULL,
  PRIMARY KEY  (`addressid`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=gbk;
 
/* Formatted on 2007/08/21 10:58 (QP5 v5.50) */
CREATE TABLE `join_1ntab` (
  `personid` int(11) NOT NULL,
  `addressid` int(11) NOT NULL,
  PRIMARY KEY  (`personid`,`addressid`),
  UNIQUE KEY `addressid` (`addressid`),
  KEY `FK6B6078C3C8DF5BFF` (`personid`),
  KEY `FK6B6078C3C2B11347` (`addressid`),
  CONSTRAINT `FK6B6078C3C2B11347` FOREIGN KEY (`addressid`) REFERENCES `address_1ntab` (`addressid`),
  CONSTRAINT `FK6B6078C3C8DF5BFF` FOREIGN KEY (`personid`) REFERENCES `person_1ntab` (`personid`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk;
 
/* Formatted on 2007/08/21 10:58 (QP5 v5.50) */
CREATE TABLE `person_1ntab` (
  `personid` int(11) NOT NULL auto_increment,
  `name` varchar(255) default NULL,
  `age` int(11) default NULL,
  PRIMARY KEY  (`personid`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=gbk;
 
 
五、映射方法
 
<hibernate-mapping>
    <class name="com.lavasoft.dx._1_n_tab.Person1ntab" table="PERSON_1ntab">
        <id name="personid">
            <generator class="identity"/>
        </id>
        <property name="name"/>
        <property name="age"/>
        <!-- 映射集合属性,join_1ntab是连接表表名-->
        <set name="addresses"
             table="join_1ntab"
                >
            <!-- column="personid"”确定PERSON_1ntab表关联到连接表的外键列名-->
            <key column="personid"/>
            <!-- column="addressid"”关联PERSON_1ntab表的Address1ntab对象的id在连接表中的列名-->
            <!-- unique="true"表示1-NPerson1ntab1Address1ntab是多”-->
            <many-to-many
                    column="addressid"
                    unique="true"
                    class="com.lavasoft.dx._1_n_tab.Address1ntab"/>
        </set>
    </class>
</hibernate-mapping>
 
<hibernate-mapping>
    <class name="com.lavasoft.dx._1_n_tab.Address1ntab" table="ADDRESS_1ntab">
        <id name="addressid">
            <generator class="identity"/>
        </id>
        <property name="addressdetail"/>
    </class>
</hibernate-mapping>
 
 
六、测试方法
 
public class Test_1ntab {
    public static void main(String[] args){
        Address1ntab add1=new Address1ntab();
        Address1ntab add2=new Address1ntab();
        Address1ntab add3=new Address1ntab();
        Person1ntab p1=new Person1ntab();
        Person1ntab p2=new Person1ntab();
 
        add1.setAddressdetail(" 郑州市经三路");
        add2.setAddressdetail(" 合肥市宿州路");
        add3.setAddressdetail(" 北京市长安路");
        p1.setName("wang");
        p1.setAge(30);
        p2.setName("lee");
        p2.setAge(50);
 
        p1.getAddresses().add(add1);
        p1.getAddresses().add(add2);
        //p2.getAddresses().add(add2);
        p2.getAddresses().add(add3);
 
        Session session= HibernateUtil.getCurrentSession();
        Transaction tx=session.beginTransaction();
        session.save(add1);
        session.save(add2);
        session.save(add3);
        session.save(p1);
        session.save(p2);
        tx.commit();
        HibernateUtil.closeSession();
    }
}
 
七、测试结果
 
1) : 正常保存.
        session.save(add1);
        session.save(add2);
        session.save(add3);
        session.save(p1);
        session.save(p2);
Hibernate: insert into PERSON_1nfk (name, age) values (?, ?)
Hibernate: insert into ADDRESS_1nfk (addressdetail) values (?)
Hibernate: insert into ADDRESS_1nfk (addressdetail) values (?)
Hibernate: update ADDRESS_1nfk set personid=? where addressid=?
Hibernate: update ADDRESS_1nfk set personid=? where addressid=?