关联映射的本质:
* 将关联关系映射到数据库,所谓的关联关系是对象模型在内存中的一个或多个引用
多对一(many-to-one)
<many-to-one>会在多的一端加入一个外键,指向一的一端,这个外键是由<many-to-one>中的column属性定义的,如果忽略了这个属性那么默认的外键与实体的属性一致。
我用user和group做示例:
多个user对于一个group
user.hbm.xml的写法:
<hibernate-mapping>
<class name="com.simlink.data.ho.User" table="t_user">
<id name="id">
<generator class="native" />
</id>
<property name="name">
<many-to-one name="group" column="groupid">
</calss>
</hibernate-mapping>
cascade(级联)
<many-to-one>有一个重要的属性cascade(级联)
* 级联的意思是指两个对象之间的操作联动关系,对一个对象执行了操作之后,对其指定的级联对象也需要执行相同的操作。
* 总共可以取值为:all、 none、 save-delete、 delete
* all代表所有情况下都执行级联操作
* none 代表所有情况下都不执行级联操作
* save-delete 代表只有在保存和删除的情况下才执行级联操作
* delete 代表只有在删除的情况下才执行级联操作
一对一(one-to-one)关联映射
有两种策略可以实现一对一的关联映射:
* 主键关联:即让两个对于具有相同的主键值,以表明它们之间的一一对于关系。数据库表不会有额外的字段来维护它们之间的关系,仅通过主键来关联。
* 唯一外键关联:外键关联,本来用于多对一配置,但是如果加上唯一的限制之后,也可以用来表示一对一关联映射。
一对一主键关联--单向
我用person和card做示例:
一个人对于一个身份证,单向关联Person---->Card
一对一主键关联映射:让两个实体对象的id保持相同,这样可以避免多余的字段被创建
person.hbm.xml的写法:
<hibernate-mapping>
<class name="com.simlink.data.ho.Person" table="t_person">
<id name="id">
<!-- person的主键来源card,也就是共享card的主键 -->
<generator class="foreign">
<param name="property">card</param>
</generator>
</id>
<property name="name">
<!-- one-to-one标签的含义,指示hibernate怎么加载它的关联对象,默认根据主键加载,constrained="true",表明当前主键上存在一个约束,person的主键作为外键参照了card -->
<one-to-one name="card" constrained="true">
</calss>
</hibernate-mapping>
一对一主键关联--双向
一个人对应一个身份证,一个身份证对应一个人,双向关联Person<---->Card
person.hbm.xml的写法不变,需要改一下card.hbm.xml
card.hbm.xml的写法:
<hibernate-mapping>
<class name="com.simlink.data.ho.Card" table="t_card">
<id name="id">
<generator class="native" />
</id>
<property name="cardid">
<one-to-one name="person">
</calss>
</hibernate-mapping>
一对一唯一外键关联映射是多对一关联映射的特例,就是将外键限制为唯一。
一对一唯一外键关联--单向
在person中多了一个外键cardno,引用card的主键,设置person里的cardid唯一
user.hbm.xml的写法:
<hibernate-mapping>
<class name="com.simlink.data.ho.Person" table="t_person">
<id name="id">
<generator class="native" />
</id>
<property name="name">
<many-to-one name="card" unique="true">
</calss>
</hibernate-mapping>
一对一唯一外键关联--双向
在card的配置中指明card连接到person的cardid字段(默认会连person的主键)
person.bbm.xml的写法同上。
card.hbm.xml的写法:
<hibernate-mapping>
<class name="com.simlink.data.ho.Card" table="t_card">
<id name="id">
<generator class="native" />
</id>
<property name="cardno">
<one-to-one name="person" property-ref="cardid">
</calss>
</hibernate-mapping>
分享到:
相关推荐
关于对Hibernate关联映射的一个总结,其中有各种映射的例子,详细用法 等
hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,包含4个说明文档,分别详细解说了hibernate关联映射的...
hibernate开发的关系: one-to-one,many-to-one,many-to-many学习经验总结
该资源包含了一些常用的hibernate关联映射实例。包括一对多,多对多,继承映射,等,是学习hibernate的必备品
Hibernate 一对一外键单向关联 Hibernate 一对一主键单向关联 Hibernate 一对一连接表单向关联 Hibernate 一对多外键单向关联 Hibernate 一对多连接表单向关联 Hibernate 多对一外键单向关联 Hibernate 多对一连接...
文件中包含关于hibernate 各个属性的解释
Hibernate关联映射.doc
hibernate关联映射详解SSH 多对多,一对多关系对象映射
Hibernate 关联关系映射分类
Hibernate关联关系映射 单向关联 │ ├─ 一对一外键单向关联 │ ├─ 一对一主键单向关联 │ ├─ 一对一连接表单向关联 │ ├─ 一对多外键单向关联 │ ├─ 一对多连接表单向关联 │ ├─ 多对一外键单向关联 │ ...
hibernate关联映射
NULL 博文链接:https://1028826685.iteye.com/blog/1536060
student与Score的Map集合关系 student与class的多对一 一对多映射
Hibernate4中映射关系图解。
该资源包括了hibernate开发中的常见关联映射的源代码,其中包括:one2one,one2many,many2many等,还有继承映射,inverse,fetch,cascade关键字的应用等,对初学者来说,是很实用的,难度适中,很好理解
Hibernate关联关系练习【全】
Hibernate双向一对一关联映射(注解版)