首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Hibernate 联系关系映射之多对一

2012-09-20 
Hibernate 关联映射之多对一Hibernate的关联映射包括:一对一(Persion - IDCard)一对多(Department - Emplo

Hibernate 关联映射之多对一

Hibernate的关联映射包括:

一对一(Persion - IDCard)

一对多(Department - Employee)

多对一(Employee - Department)

多对多(Teacher - Student)

组件映射(User - Name)

集合映射(Set、List、Map)

inverse和cascade(Employee - Department)

?

本文以多对一为例:

多个员工对应一个部门(Employee - Department)

映射文件 <many-to-one name="depart" column="depart_id">

?

实体类:Department.java 和 Employee.java

CREATE TABLE `department` (  `id` int(11) NOT NULL auto_increment,  `name` varchar(255) default NULL,  PRIMARY KEY  (`id`)) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1CREATE TABLE `employee` (  `id` int(11) NOT NULL auto_increment,  `name` varchar(255) default NULL,  `dept` int(11) default NULL,  PRIMARY KEY  (`id`),  KEY `FK4AFD4ACE66A70F4E` (`dept`),  CONSTRAINT `FK4AFD4ACE66A70F4E` FOREIGN KEY (`dept`) REFERENCES `department` (`id`)) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1 

mysql> desc department;
+-------+--------------+------+-----+---------+----------------+
| Field | Type???????? | Null | Key | Default | Extra????????? |
+-------+--------------+------+-----+---------+----------------+
| id??? | int(11)????? | NO?? | PRI | NULL??? | auto_increment |
| name? | varchar(255) | YES? |???? | NULL??? |??????????????? |
+-------+--------------+------+-----+---------+----------------+
mysql> desc employee;
+-------+--------------+------+-----+---------+----------------+
| Field | Type???????? | Null | Key | Default | Extra????????? |
+-------+--------------+------+-----+---------+----------------+
| id??? | int(11)????? | NO?? | PRI | NULL??? | auto_increment |
| name? | varchar(255) | YES? |???? | NULL??? |??????????????? |
| dept? | int(11)????? | YES? | MUL | NULL??? |??????????????? |
+-------+--------------+------+-----+---------+----------------+

?

?

?

?

热点排行