[NHibernate]关联映射

mac2022-06-30  23

系列文章

[Nhibernate]体系结构

[NHibernate]ISessionFactory配置

[NHibernate]持久化类(Persistent Classes)

[NHibernate]O/R Mapping基础

[NHibernate]集合类(Collections)映射 

引言

单向关联是最常用的也是最难正确使用的。在本文中会逐个经历规范的案例,从单向映射开始,然后涉及双向的案例。我们会在所有的例子中hi用Person和Address。例子中没有包括命名空间和程序集,我们把关注点放在重要的方面。

我们通过是否使用表连接和多样性(单向或双向)分类关联。

在传统的数据模型中允许为空的外键是不适用的,所以我们的例子中没有使用允许为空的外键,在NHibernate中这不是必须的,如果你删除控制的约束,映射会照常工作。

单向关联

多对一(many to one)

一对一(one to one)

一对多(one to many)

使用表连接的单向关联

多对一(many to one)

一对一(one to one)

一对多(one to many)

多对多(many to many)

双向关联

一对多(one to many)/多对一(many to one)

双向的一对多(one to many)关联是普通的关联类型。(这是标准的parent/child关系)

1 <class name="Person"> 2 <id name="Id" column="personId"> 3 <generator class="native" /> 4 </id> 5 <many-to-one name="Address" 6 column="addressId" 7 not-null="true" 8 /> 9 </class> 10 <class name="Address"> 11 <id name="Id" column="addressId"> 12 <generator class="native" /> 13 </id> 14 <set name="People" inverse="true"> 15 <key column="addressId" /> 16 <one-to-many class="Person" /> 17 </set> 18 </class> 19 create table Person 20 ( 21 personId bigint not null primary key, 22 addressId bigint not null 23 ) 24 create table Address 25 ( 26 addressId bigint not null primary key 27 )

     一对一(one to one)

使用表连接的双向关联

一对多(one to many)/多对一(many to one)

一对一(one to one)

多对多(many to many)

总结

这里对知识点有个大概的了解,具体应用还需在后续的文章中,通过例子来说明。

本文来自《NHibernat 中文文档》

博客地址:http://www.cnblogs.com/wolf-sun/ 博客版权:如果文中有不妥或者错误的地方还望高手的你指出,以免误人子弟。如果觉得本文对你有所帮助不如【推荐】一下!如果你有更好的建议,不如留言一起讨论,共同进步! 再次感谢您耐心的读完本篇文章。

转载于:https://www.cnblogs.com/Jeely/p/11357874.html

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)