posts - 200, comments - 8, trackbacks - 0, articles - 0

Hibernate 一对多外键双向关联

Posted on 2011-11-18 17:02 鑫龙 阅读(400) 评论(0)  编辑 收藏 引用 所属分类: 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 (?, ?)

 八、附加
 inverse="true"表示pojo由多方维护,
 inverse="false"表示pojo由一方维护。

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理