Hibernate 一对多外键双向关联

Hibernate 一对多外键双向关联
 
一、模型介绍
 
一个人(Person)对应多个地址(Address)。
 
二、实体(省略gettersetter方法)
 
public class Person1nfk_sx implements Serializable {
    private int personid;
    private String name;
    private int age;
    private Set addresses=new HashSet();
 
public class Address1nfk_sx implements Serializable {
    private int addressid;
    private String addressdetail;
    private Person1nfk_sx person1nfkSx;
 
三、表模型
 
mysql> desc person_1nfk_sx;
+----------+--------------+------+-----+---------+----------------+
| 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_1nfk_sx;
+---------------+--------------+------+-----+---------+----------------+
| Field         | Type         | Null | Key | Default | Extra          |
+---------------+--------------+------+-----+---------+----------------+
| addressid     | int(11)      | NO   | PRI | NULL    | auto_increment |
| addressdetail | varchar(255) | YES  |     | NULL    |                |
| personid      | int(11)      | NO   | MUL |         |                |
+---------------+--------------+------+-----+---------+----------------+
 
四、生成的SQL脚本
 
  /* Formatted on 2007/08/22 17:42 (QP5 v5.50) */
  CREATE TABLE `address_1nfk` (
    `addressid` int(11) NOT NULL auto_increment,
    `addressdetail` varchar(255) default NULL,
    `personid` int(11) default NULL,
    PRIMARY KEY  (`addressid`),
    KEY `FK9B93456DA6D6C1F5` (`personid`),
    CONSTRAINT `FK9B93456DA6D6C1F5` FOREIGN KEY (`personid`) REFERENCES `person_1nfk` (`personid`)
  ) ENGINE=InnoDB DEFAULT CHARSET=gbk;
 
 
  /* Formatted on 2007/08/22 17:42 (QP5 v5.50) */
  CREATE TABLE `person_1nfk` (
    `personid` int(11) NOT NULL auto_increment,
    `name` varchar(255) default NULL,
    `age` int(11) default NULL,
    PRIMARY KEY  (`personid`)
  ) ENGINE=InnoDB DEFAULT CHARSET=gbk;
 
五、映射方法
 
<hibernate-mapping>
    <class name="com.lavasoft.sx._1_n_fk.Person1nfk_sx" table="PERSON_1nfk_sx">
        <id name="personid">
            <generator class="identity"/>
        </id>
        <property name="name"/>
        <property name="age"/>
        <!--映射集合属性,关联到持久化类-->
        <set name="addresses" inverse="true" cascade="all">
            <!--column用于指定外键列名-->
            <key column="personid" not-null="true"/>
            <!--映射关联类-->
            <one-to-many class="com.lavasoft.sx._1_n_fk.Address1nfk_sx"/>
        </set>
    </class>
</hibernate-mapping>
 
<hibernate-mapping>
    <class name="com.lavasoft.sx._1_n_fk.Address1nfk_sx" table="ADDRESS_1nfk_sx">
        <id name="addressid">
            <generator class="identity"/>
        </id>
        <property name="addressdetail"/>
        <!--映射关联属性,column属性指定外键列名-->
        <many-to-one name="person1nfk"
                     class="com.lavasoft.sx._1_n_fk.Person1nfk_sx"
                     fetch="select"
                     cascade="save-update">
            <column name="personid" not-null="true"/>
        </many-to-one>
    </class>
</hibernate-mapping>
 
六、测试方法
 
public class Test_1nfk_sx {
    public static void main(String[] args){
        Address1nfk_sx add1=new Address1nfk_sx();
        Address1nfk_sx add2=new Address1nfk_sx();
        Person1nfk_sx p=new Person1nfk_sx();
 
        add1.setAddressdetail("郑州市经三路");
        add2.setAddressdetail("合肥市宿州路");
        p.setName("wang");
        p.setAge(30);
 
        p.getAddresses().add(add1);
        p.getAddresses().add(add2);
        add1.setPerson1nfk(p);
        add2.setPerson1nfk(p);
 
        Session session= HibernateUtil.getCurrentSession();
        Transaction tx=session.beginTransaction();
        session.save(p);
        session.saveOrUpdate(add1);
        session.saveOrUpdate(add2);
        tx.commit();
        HibernateUtil.closeSession();
    }
}
 
七、测试结果
 
1) :正常保存.
        session.save(p);
        session.saveOrUpdate(add1);
        session.saveOrUpdate(add2);
 
Hibernate: insert into PERSON_1nfk_sx (name, age) values (?, ?)
Hibernate: insert into ADDRESS_1nfk_sx (addressdetail, personid) values (?, ?)
Hibernate: insert into ADDRESS_1nfk_sx (addressdetail, personid) values (?, ?)
 
 
 


本文转自 leizhimin 51CTO博客,原文链接:http://blog.51cto.com/lavasoft/39336,如需转载请自行联系原作者

 
上一篇:Hibernate 实体关联关系映射----总结


下一篇:Tomcat 6 虚拟目录配置方法