Hibernate 一对一连接表单向关联

Hibernate 一对一连接表单向关联
 
    这种情况很少见,但Hibernate同样允许采用连接表关联1-1.有连接表的1-1同样只需要将N-1many-to-one元素增加unique="true"属性即可。
 
一、模型介绍
 
一个人(Person)对应一个地址(Address)。
 
二、实体(省略gettersetter方法)
 
public class Person11tab {
    private int personid;
    private String name;
    private int age;
    private Address11tab address11tab;
 
public class Address11tab {
    private int addressid;
    private String addressdetail;
 
三、表模型
 
mysql> desc address_11tab;
+---------------+--------------+------+-----+---------+----------------+
| Field         | Type         | Null | Key | Default | Extra          |
+---------------+--------------+------+-----+---------+----------------+
| addressid     | int(11)      | NO   | PRI | NULL    | auto_increment |
| addressdetail | varchar(255) | YES  |     | NULL    |                |
+---------------+--------------+------+-----+---------+----------------+
 
mysql> desc join_11tab;
+--------------+---------+------+-----+---------+-------+
| Field        | Type    | Null | Key | Default | Extra |
+--------------+---------+------+-----+---------+-------+
| personid     | int(11) | NO   | PRI |         |       |
| address11tab | int(11) | YES  | UNI | NULL    |       |
+--------------+---------+------+-----+---------+-------+
 
mysql> desc person_11tab;
+----------+--------------+------+-----+---------+----------------+
| Field    | Type         | Null | Key | Default | Extra          |
+----------+--------------+------+-----+---------+----------------+
| personid | int(11)      | NO   | PRI | NULL    | auto_increment |
| name     | varchar(255) | YES  |     | NULL    |                |
| age      | int(11)      | YES  |     | NULL    |                |
+----------+--------------+------+-----+---------+----------------+
 
四、生成的SQL脚本
 
/* Formatted on 2007/08/20 16:52 (QP5 v5.50) */
CREATE TABLE `join_11tab` (
  `personid` int(11) NOT NULL,
  `address11tab` int(11) default NULL,
  PRIMARY KEY  (`personid`),
  UNIQUE KEY `address11tab` (`address11tab`),
  KEY `FK6B44BE20C4CC3D33` (`address11tab`),
  KEY `FK6B44BE209049BB1F` (`personid`),
  CONSTRAINT `FK6B44BE209049BB1F` FOREIGN KEY (`personid`) REFERENCES `person_11tab` (`personid`),
  CONSTRAINT `FK6B44BE20C4CC3D33` FOREIGN KEY (`address11tab`) REFERENCES `address_11tab` (`addressid`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk;
 
/* Formatted on 2007/08/20 16:53 (QP5 v5.50) */
CREATE TABLE `address_11tab` (
  `addressid` int(11) NOT NULL auto_increment,
  `addressdetail` varchar(255) default NULL,
  PRIMARY KEY  (`addressid`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=gbk;
 
/* Formatted on 2007/08/20 16:53 (QP5 v5.50) */
CREATE TABLE `person_11tab` (
  `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;
 
 
五、映射方法:在Person中添加Address属性,映射配置为:
        <!--使用join元素显式确定链接表-->
        <join table="join_11tab">
            <key column="personid"/>
            <!--映射1-1关联属性,其中unique=true”属性确定为“1-1-->
            <many-to-one name="address11tab" unique="true"/>
        </join>
 
<hibernate-mapping>
    <class name="com.lavasoft.dx._1_1_tab.Person11tab" table="PERSON_11tab">
        <id name="personid">
            <generator class="identity"/>
        </id>
        <property name="name"/>
        <property name="age"/>
        <!--使用join元素显式确定链接表-->
        <join table="join_11tab">
            <key column="personid"/>
            <!--映射1-1关联属性,其中unique=true”属性确定为“1-1-->
            <many-to-one name="address11tab" unique="true"/>
        </join>
    </class>
</hibernate-mapping>
 
<hibernate-mapping>
    <class name="com.lavasoft.dx._1_1_tab.Address11tab" table="ADDRESS_11tab">
        <id name="addressid">
            <generator class="identity"/>
        </id>
        <property name="addressdetail"/>
    </class>
</hibernate-mapping>
 
 
六、测试方法
 
public class Test_11tab {
    public static void main(String[] args){
        Person11tab p1=new Person11tab();
 
        p1.setAge(21);
        p1.setName("p1");
 
        Address11tab add1=new Address11tab();
        add1.setAddressdetail("郑州市经三路");
 
        p1.setAddress11tab(add1);
 
        Session session= HibernateUtil.getCurrentSession();
        Transaction tx=session.beginTransaction();
        session.save(add1);
        session.save(p1);
        tx.commit();
        HibernateUtil.closeSession();
    }
}
 
七、测试结果
 
1) :正常保存推荐这么干!
        session.save(add1);
        session.save(p1);
 
Hibernate: insert into ADDRESS_11tab (addressdetail) values (?)
Hibernate: insert into PERSON_11tab (name, age) values (?, ?)
Hibernate: insert into join_11tab (address11tab, personid) values (?, ?)
 
2) :正常保存.
        session.save(p1);
        session.save(add1);
 
Hibernate: insert into PERSON_11tab (name, age) values (?, ?)
Hibernate: insert into join_11tab (address11tab, personid) values (?, ?)
Hibernate: insert into ADDRESS_11tab (addressdetail) values (?)
Hibernate: update join_11tab set address11tab=? where personid=?
 
3) :正常保存.
//        session.save(p1);
        session.save(add1);
 
Hibernate: insert into ADDRESS_11tab (addressdetail) values (?)
 
4) : 发生异常,不能保存.
        session.save(p1);
//        session.save(add1);
 
Hibernate: insert into PERSON_11tab (name, age) values (?, ?)
Hibernate: insert into join_11tab (address11tab, personid) values (?, ?)
Exception in thread "main" org.hibernate.TransientObjectException: com.lavasoft.dx._1_1_tab.Address11tab
 


上一篇:解决方案应用实例 |借力业务中台,特步走上数字化转型的快车道


下一篇:Muduo 网络编程示例之零:前言