版权声明:原创作品,如需转载,请与作者联系。否则将追究法律责任。
|
原文地址: http://lavasoft.blog.51cto.com/62575/39318
Hibernate
一对多连接表单向关联
|
|
|
一、模型介绍
|
|
一个人(Person)对应多个地址(Address),比如家庭地址、公司地址。
|
|
二、实体(省略getter、setter方法)
|
三、表模型
|
|
mysql> desc join_1ntab;
|
+-----------+---------+------+-----+---------+-------+
|
| Field | Type | Null | Key | Default | Extra |
|
+-----------+---------+------+-----+---------+-------+
|
| personid | int(11) | NO | PRI | | |
|
| addressid | int(11) | NO | PRI | | |
|
+-----------+---------+------+-----+---------+-------+
|
|
mysql> desc person_1ntab;
|
+----------+--------------+------+-----+---------+----------------+
|
| 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_1ntab;
|
+---------------+--------------+------+-----+---------+----------------+
|
| Field | Type | Null | Key | Default | Extra |
|
+---------------+--------------+------+-----+---------+----------------+
|
| addressid | int(11) | NO | PRI | NULL | auto_increment |
|
| addressdetail | varchar(255) | YES | | NULL | |
|
+---------------+--------------+------+-----+---------+----------------+
|
|
四、生成的SQL脚本
|
|
/* Formatted on 2007/08/21 10:58 (QP5 v5.50) */
|
CREATE TABLE `address_1ntab` (
|
`addressid` int(11) NOT NULL auto_increment,
|
`addressdetail` varchar(255) default NULL,
|
PRIMARY KEY (`addressid`)
|
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=gbk;
|
|
/* Formatted on 2007/08/21 10:58 (QP5 v5.50) */
|
CREATE TABLE `join_1ntab` (
|
`personid` int(11) NOT NULL,
|
`addressid` int(11) NOT NULL,
|
PRIMARY KEY (`personid`,`addressid`),
|
UNIQUE KEY `addressid` (`addressid`),
|
KEY `FK6B6078C3C8DF5BFF` (`personid`),
|
KEY `FK6B6078C3C2B11347` (`addressid`),
|
CONSTRAINT `FK6B6078C3C2B11347` FOREIGN KEY (`addressid`) REFERENCES `address_1ntab` (`addressid`),
|
CONSTRAINT `FK6B6078C3C8DF5BFF` FOREIGN KEY (`personid`) REFERENCES `person_1ntab` (`personid`)
|
) ENGINE=InnoDB DEFAULT CHARSET=gbk;
|
|
/* Formatted on 2007/08/21 10:58 (QP5 v5.50) */
|
CREATE TABLE `person_1ntab` (
|
`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;
|
|
|
五、映射方法
|