Hibernate笔记=>一对多关系的映射
http://blog.sina.com.cn/s/blog_3d7bed65010004kq.html
Hibernate笔记=>对象与关系的映射基础
http://blog.sina.com.cn/s/blog_3d7bed65010004kp.html~type=v5_one&label=rela_prevarticle
Hibernate笔记=>继承关系的映射
http://blog.sina.com.cn/s/blog_3d7bed65010004lx.html~type=v5_one&label=rela_nextarticle
Hibernate笔记=>检索相关细节问题
http://blog.sina.com.cn/s/blog_3d7bed65010004o9.html~type=v5_one&label=rela_nextarticle
-----------------------------------------------------------------
Hibernate之映射继承关系(六)
http://tech.ccidnet.com/art/297/20050922/339201_1.html
Hibernate 实体关联关系映射----总结
http://lavasoft.blog.51cto.com/62575/39398
-----------------------------------------------------------------
总结:
一.类的关联关系,在hibernate中反映
1.单向关联与双向关联
单向关联是类的一方拥有对方的引用.
双向关联是类的双方都拥有对方的引用.
2.关系分类:
1对多: custom-->order; custom中有存放order的list对象的属性.
多对1: order-->custom; order中有custom对象的属性.
多对多: 1个老师有多个学生,1学生又有多个老师.
1对1: 是特殊的多对1关系.
3.表的实现:
[1]外键关联:多端有外键去关联1端.
一个人(Person)对应多个地址(Address)
mysql> desc address_1nfk;
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| addressid | int(11) | NO | PRI | NULL | auto_increment |
| addressdetail | varchar(255) | YES | | NULL | |
| personid | int(11) | YES | MUL | NULL | |
+---------------+--------------+------+-----+---------+----------------+
mysql> desc person_1nfk;
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| personid | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
+----------+--------------+------+-----+---------+----------------+
多个人(Person)对应一个地址(Address)
mysql> desc address_n1kf;
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| addressid | int(11) | NO | PRI | NULL | auto_increment |
| addressdetail | varchar(255) | YES | | NULL | |
+---------------+--------------+------+-----+---------+----------------+
mysql> desc person_n1kf;
+-----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+----------------+
| personid | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| addressId | int(11) | YES | MUL | NULL | |
+-----------+--------------+------+-----+---------+----------------+
[2]主键作外键关联:(只适用于一对一的关系中)
一个人(Person)对应一个地址(Address)
mysql> desc address_11pk;
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| addressid | int(11) | NO | PRI | NULL | auto_increment |
| addressdetail | varchar(255) | YES | | NULL | |
+---------------+--------------+------+-----+---------+----------------+
mysql> desc person_11pk;
+----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| personid | int(11) | NO | PRI | | |
| name | varchar(255) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
+----------+--------------+------+-----+---------+-------+
[3]连接表关联:建一连接表,内容是person表和address表的id,(多对多一定是用连接表)
多对多的例子
mysql> desc person_nn;
+----------+--------------+------+-----+---------+----------------+
| 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_nn;
+-----------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+-------+
| personid | int(11) | NO | PRI | | |
| addressid | int(11) | NO | PRI | | |
+-----------+---------+------+-----+---------+-------+
mysql> desc person_nn;
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| personid | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
+----------+--------------+------+-----+---------+----------------+
二,类的继承关系在hibernate中的表示
1. 1子类一张表 不支持多态查询。
2. 全一张表 支持多态,查询最快,维护难。
3. 1类一表 支持多态,维护易。