hibernate学习之——one-to-many(双向一对多关系)
来源:互联网 发布:若宇网络 编辑:程序博客网 时间:2024/05/20 06:41
hibernate自主学习,汤阳光hibernate视频例子。
这里有两个实体类Department部门(one),Employee员工(many)。
Department配置文件:
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.bean"><class name="Department" table="department"><id name="id" column="id" type="int"><generator class="native" /></id><property name="name" type="string" column="name" length="30"></property><!-- employees属性,employees集合,表达本类与Employee集合的一对多关系,对应数据库表是employeekey子元素column:对方表中的外键列(many的一方)class属性:关联的实体类(many的一方)inverse属性:默认为false:表示本方维护关联关系true:表示本方放弃维护关联关系 --><set name="employees" table="employee" inverse="false"><key column="departmentId"></key><one-to-many class="Employee" /></set></class></hibernate-mapping>
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.bean"><class name="Employee" table="employee"><id name="id" column="id" type="int"><generator class="native" /></id><property name="name" type="string" column="name" length="30"></property><!-- class属性:指定关联的类name属性:关联类在数据库中的命名column:外键列名。必须与关联类中key子元素column相同。 --><many-to-one class="Department" name="department" column="departmentId"></many-to-one></class></hibernate-mapping>
解释:
department中定义了一个自动增长主键id,和一个string类型的部门name。
employee中也定义了一个主键id,string类型的员工name,以及一个外键departmentId,关联到department表中主键id,实现(部门—员工)的一对多关系。
department配置中,department是one的一方,所以配置文件属性为<one-to-many class="Employee"/>,一个部门对应多个员工。
employee,员工是many的一个方,所以是<many-to-one class="Department" />,多个员工对应一个部门。
0 0
- hibernate学习之——one-to-many(双向一对多关系)
- Hibernate学习笔记:一对多的关联关系(one-to-many) 双向关联
- Hibernate学习笔记:一对多的关联关系(one-to-many) 双向关联
- hibernate学习之——many-to-many(双向多对多关系)
- hibernate映射技巧one-to-many(双向一对多)
- hibernate 一对多(one-to-many)双向关联
- Hibernate One-to-Many Mappings 一对多关系映射
- hibernate学习5之one-to-many双向关联
- hibernate学习5之one-to-many双向关联
- Entity Framework 实践系列 —— 搞好关系 - 生儿育女(一对多,one-to-many)
- Hibernate 多表关联映射- 一对多关系映射(one-to-many)
- 码农小汪-Hibernate学习4-关联关系映射many-to-one,many-to-one,one-to-many
- Hibernate学习之级联操作2——一对多双向自身关联关系
- hibernate学习2之many-to-one
- hibernate映射技巧one-to-many(单项一对多)
- hibernate中一对多(one-to-many)的实例
- Hibernate单向一对多关联(Unidirectional One-To-Many)要点
- hibernate映射技巧one-to-many(单项一对多)
- C++之练习题20
- 用数组公式从一列中提取包含指定内容的数据
- HM学习心得1
- 2>&1 and 1>&2
- (五)u-boot2013.01.01 for s5pv210:《移植前的准备及u-boot初编译》
- hibernate学习之——one-to-many(双向一对多关系)
- fghgfhfghfghfghfghfgh
- 编辑文章 - 博客频道 - CSDN.NET
- Android Studio中git新建远程(remote)branch?
- HDUJ 1242 Rescue 搜索
- SqlServer_关联表、链接表、合并表-多对多表关系的解决方案;
- C++ 四种强制类型转换
- dfgdfhgfhgfhfh
- 位运算小技巧。