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??? |??????????????? |
+-------+--------------+------+-----+---------+----------------+
?
?
?
?