首页 > 学院 > 开发设计 > 正文

Hibernate 一对一连接表双向关联

2019-11-10 20:24:03
字体:
来源:转载
供稿:网友
Hibernate一对一连接表双向关联
 
 
一、模型介绍
 
一个人(Person)对应一个地址(Address)。
 
二、实体(省略getter、setter方法)
 
public class Person11tab_sx {
    PRivate int personid;
    private String name;
    private int age;
    private Address11tab_sx address11tab_sx;
 
public class Address11tab_sx {
    private int addressid;
    private String addressdetail;
    private Person11tab_sx person11tab_sx;
 
三、表模型
 
MySQL> desc person_11tab_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 join_11tab_sx;
+-----------+---------+------+-----+---------+-------+
| Field     | Type    | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+-------+
| addressid | int(11) | NO   | UNI |         |       |
| personid  | int(11) | NO   | PRI |         |       |
+-----------+---------+------+-----+---------+-------+
 
mysql> desc address_11tab_sx;
+---------------+--------------+------+-----+---------+----------------+
| Field         | Type         | Null | Key | Default | Extra          |
+---------------+--------------+------+-----+---------+----------------+
| addressid     | int(11)      | NO   | PRI | NULL    | auto_increment |
| addressdetail | varchar(255) | YES  |     | NULL    |                |
+---------------+--------------+------+-----+---------+----------------+
 
四、生成的SQL脚本
 
/* Formatted on 2007/08/22 17:35 (QP5 v5.50) */
CREATE TABLE `person_11tab_sx` (
  `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;
 
/* Formatted on 2007/08/22 17:34 (QP5 v5.50) */
CREATE TABLE `address_11tab_sx` (
  `addressid` int(11) NOT NULL auto_increment,
  `addressdetail` varchar(255) default NULL,
  PRIMARY KEY  (`addressid`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk; /* Formatted on 2007/08/22 18:35 (QP5 v5.50) */CREATE TABLE `join_11tab_sx` (  `addressid` int(11) NOT NULL,  `personid` int(11) NOT NULL,  PRIMARY KEY  (`personid`),  UNIQUE KEY `addressid` (`addressid`),  UNIQUE KEY `personid` (`personid`),  KEY `FKF4AA80E44327AAB6` (`personid`),  KEY `FKF4AA80E460C0C9F0` (`addressid`),  CONSTRAINT `FKF4AA80E460C0C9F0` FOREIGN KEY (`addressid`) REFERENCES `address_11tab_sx` (`addressid`),  CONSTRAINT `FKF4AA80E44327AAB6` FOREIGN KEY (`personid`) REFERENCES `person_11tab_sx` (`personid`)) ENGINE=InnoDB DEFAULT CHARSET=gbk; 
 
 
五、映射方法
 
<hibernate-mapping>
    <class name="com.lavasoft.sx._1_1_tab.Person11tab_sx" table="PERSON_11tab_sx">
        <id name="personid">
            <generator class="identity"/>
        </id>
        <property name="name"/>
        <property name="age"/>
        <join table="join_11tab_sx"
              optional="true">
            <key column="personid"
                 unique="true"/>
            <many-to-one name="address11tab_sx"
                         column="addressid"
                         not-null="true"
                         unique="true"/>
        </join>
    </class>
</hibernate-mapping>
 
<hibernate-mapping>
    <class name="com.lavasoft.sx._1_1_tab.Address11tab_sx" table="ADDRESS_11tab_sx">
        <id name="addressid">
            <generator class="identity"/>
        </id>
        <property name="addressdetail"/>
        <join table="join_11tab_sx"
              optional="true"
              inverse="true">
            <key column="addressid"
                 unique="true"/>
            <many-to-one name="person11tab_sx" column="personid"
                         not-null="true" unique="true"/>
        </join>
    </class>
</hibernate-mapping>
 
六、测试方法
 
public class Test_11tab_sx {
    public static void main(String[] args){
        Address11tab_sx add = new Address11tab_sx();
        Person11tab_sx p = new Person11tab_sx();
 
        add.setAddressdetail("郑州市经三路");
        p.setAge(12);
        p.setName("wudalang");
 
        add.setPerson11tab_sx(p);
        p.setAddress11tab_sx(add);
 
        session session = HibernateUtil.getCurrentSession();
        Transaction tx = session.beginTransaction();
        session.saveOrUpdate(p);
        session.saveOrUpdate(add);
        tx.commit();
        HibernateUtil.closeSession();
    }
}
 
七、测试结果
 
1) :正常保存.
        session.saveOrUpdate(p);
        session.saveOrUpdate(add);
 
Hibernate: insert into PERSON_11tab_sx (name, age) values (?, ?)
Hibernate: insert into ADDRESS_11tab_sx (addressdetail) values (?)
Hibernate: insert into join_11tab_sx (addressid, personid) values (?, ?)

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表