`

Hibernate 一对一连接表双向关联

 
阅读更多
一、模型介绍
 
一个人(Person)对应一个地址(Address)。
 
二、实体(省略gettersetter方法)
 
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 (?, ?)
分享到:
评论

相关推荐

    Hibernate关联映射

    Hibernate 一对一外键单向关联 Hibernate 一对一主键单向关联 Hibernate 一对一连接表单向...Hibernate 一对一连接表双向关联 Hibernate 一对多外键双向关联 Hibernate 一对多连接表双向关联 Hibernate 多对多双向关联

    Hibernate关联关系映射目录

    Hibernate关联关系映射 单向关联 │ ├─ 一对一外键单向关联 │ ├─ 一对一主键单向关联 ...├─ 一对一连接表双向关联 ├─ 一对多外键双向关联 ├─ 一对多连接表双向关联 └─ 多对多双向关联

    Hibernate ORM - 一对多双向连接表关联关系

    NULL 博文链接:https://dreamzhong.iteye.com/blog/1201437

    hibernate学习笔记

    hibernate一对一主键关联映射(双向关联Person&lt;----&gt;IdCard) 9 hibernate一对一唯一外键关联映射(单向关联Person----&gt;IdCard) 10 hibernate一对一唯一外键关联映射(双向关联Person&lt;----&gt;IdCard) 11 session ...

    Hibernate_Annotation关联映射

    一对一关联可能是双向的,在双向关联中,有且仅有一端作为主体(owner)端存在:主体端负责维护联接列(即更新),对于不需要维护这种关系的从表则通过mappedNy属性进行声明。mappedBy的值指向主体的关联属性。例子...

    Hibernate+中文文档

    7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂...

    HibernateAPI中文版.chm

    7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂...

    hibernate3.2中文文档(chm格式)

    7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂...

    Hibernate注释大全收藏

    Hibernate注释大全收藏 声明实体Bean @Entity public class Flight implements Serializable { Long id; @Id public Long getId() { return id; } public void setId(Long id) { this.id...一对一 使用 @OneToOne...

    Hibernate中文详细学习文档

    7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂...

    Hibernate 中文 html 帮助文档

    7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂的...

    Hibernate教程

    8.5. 使用连接表的双向关联(Bidirectional associations with join tables) 8.5.1. 一对多(one to many) /多对一( many to one) 8.5.2. 一对一(one to one) 8.5.3. 多对多(many to many) 9. 组件...

    hibernate 体系结构与配置 参考文档(html)

    使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂的...

    最全Hibernate 参考文档

    7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 8. 组件...

    Hibernate_3.2.0_符合Java习惯的关系数据库持久化

    7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂...

    Hibernate3的帮助文档

    8.5. 使用连接表的双向关联(Bidirectional associations with join tables) 8.5.1. 一对多(one to many) /多对一( many to one) 8.5.2. 一对一(one to one) 8.5.3. 多对多(many to many) 9. 组件...

    精通 Hibernate:Java 对象持久化技术详解(第2版).part2

     7.2 映射一对多双向关联关系  7.2.1 元素的inverse属性  7.2.2 级联删除  7.2.3 父子关系  7.3 映射一对多双向自身关联关系  7.4 改进持久化类  7.5 小结  7.6 思考题 第8章 通过Hibernate操纵对象(上) ...

    hibernate3.04中文文档.chm

    8.5. 使用连接表的双向关联(Bidirectional associations with join tables) 8.5.1. 一对多(one to many) /多对一( many to one) 8.5.2. 一对一(one to one) 8.5.3. 多对多(many to many) 9. 组件...

    hibernate 框架详解

    使用连接表的双向关联(Bidirectional associations with join tables) 8.5.1. 一对多(one to many) /多对一( many to one) 8.5.2. 一对一(one to one) 8.5.3. 多对多(many to many) 9. 组件...

    Hibernate3+中文参考文档

    7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 8. 组件...

Global site tag (gtag.js) - Google Analytics