Hibernate 一对一连接表单向关联

来源:互联网 发布:数据库挂起 编辑:程序博客网 时间:2024/05/31 19:32
版权声明:原创作品,如需转载,请与作者联系。否则将追究法律责任。
原文地址:http://lavasoft.blog.51cto.com/62575/39312
Hibernate 一对一连接表单向关联
 
    这种情况很少见,但Hibernate同样允许采用连接表关联1-1.有连接表的1-1同样只需要将N-1many-to-one元素增加unique="true"属性即可。
 
一、模型介绍
 
一个人(Person)对应一个地址(Address)。
 
二、实体(省略gettersetter方法)

三、表模型
 
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属性,映射配置为:

 

六、测试方法

 

七、测试结果

本文出自 “熔 岩” 博客,转载请与作者联系!

附件下载:
  _1_1_tab

 

 

原创粉丝点击