many-to-one:描述多对一的一种数据模型,它指定many一方是不能独立存在的,我个人认为many-to-one是NHB中保证数据有效性的最有用的一种映射,通过使用many-to-one能有效的防治孤儿记录被写入到数据表中。在本文描述的例子中,Student类和Classes类之间是多对一关系.
通过many-to-one元素,可以定义一种常见的与另一个持久化类的关联。这种关系模型是多对一关联。(实际上是一个对象引用。)在映射文件中用many-to-one标签描述此种关系.
![[hibernate]many-to-one/one-to-many/many-to-many关系映射 [hibernate]many-to-one/one-to-many/many-to-many关系映射](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![[hibernate]many-to-one/one-to-many/many-to-many关系映射 [hibernate]many-to-one/one-to-many/many-to-many关系映射](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![[hibernate]many-to-one/one-to-many/many-to-many关系映射 [hibernate]many-to-one/one-to-many/many-to-many关系映射](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![[hibernate]many-to-one/one-to-many/many-to-many关系映射 [hibernate]many-to-one/one-to-many/many-to-many关系映射](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![[hibernate]many-to-one/one-to-many/many-to-many关系映射 [hibernate]many-to-one/one-to-many/many-to-many关系映射](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![[hibernate]many-to-one/one-to-many/many-to-many关系映射 [hibernate]many-to-one/one-to-many/many-to-many关系映射](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![[hibernate]many-to-one/one-to-many/many-to-many关系映射 [hibernate]many-to-one/one-to-many/many-to-many关系映射](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![[hibernate]many-to-one/one-to-many/many-to-many关系映射 [hibernate]many-to-one/one-to-many/many-to-many关系映射](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![[hibernate]many-to-one/one-to-many/many-to-many关系映射 [hibernate]many-to-one/one-to-many/many-to-many关系映射](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![[hibernate]many-to-one/one-to-many/many-to-many关系映射 [hibernate]many-to-one/one-to-many/many-to-many关系映射](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![[hibernate]many-to-one/one-to-many/many-to-many关系映射 [hibernate]many-to-one/one-to-many/many-to-many关系映射](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![[hibernate]many-to-one/one-to-many/many-to-many关系映射 [hibernate]many-to-one/one-to-many/many-to-many关系映射](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
1.name:属性名。指出many一方的类用哪个属性和one一方的类关联.
2.column:字段名(可选).指出many一方的类对应的数据表用哪个列和one一方的类对应的数据表关联(两表之间存在外键关联);
3.class:关联的类的名字(可选 - 默认是通过反射得到属性类型);
4.cascade:指明哪些操作会从父对象级联到关联的对象(可选).cascade属性允许下列值:: all
, save-update
, delete
, none
. 设置除了none
以外的其它值会传播特定的操作到关联的(子)对象中。
5.outer-join:当设置hibernate.use_outer_join
的时候,对这个关联允许外连接抓取(可选 - 默认为 auto
).outer-join参数允许下列三个不同值: auto(使用外连接抓取关联(对象),如果被关联的对象没有代理(proxy) ),true(一直使用外连接来抓取关联),false(永远不使用外连接来抓取关联);
6.update,insert:指定对应的字段是否在用于UPDATE
INSERT
的SQL语句中包含。如果二者都是false
,则这是一个纯粹的“外源性(derived)”关联,它的值是通过映射到同一个(或多个)字段的某些其他属性得到的,或者通过trigger(除法器),或者是其他程序(可选 - 默认为 true
)
7.property-ref:指定关联类的一个属性,这个属性将会和本外键相对应。如果没有指定,会使用对方关联类的主键(可选).property-ref属性只应该用来对付老旧的数据库系统,可能出现外键指向对方关联表的是个非主键字段(但是应该是一个惟一关键字)的情况。这是一种十分丑陋的关系模型。比如说,假设Product类有一个惟一的序列号,它并不是主键;
8.access:NHibernate 用来访问属性的策略(可选 - 默认为property
)
9.unique:允许产生外键列唯一约束的数据库定义语言(DDL)(可选)