学习笔记18(NHibernate中many-to-one映射关系)

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关系映射 < many-to-one
[hibernate]many-to-one/one-to-many/many-to-many关系映射<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
name ="propertyName" (1)
[hibernate]many-to-one/one-to-many/many-to-many关系映射<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>column</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
="column_name" (2)
[hibernate]many-to-one/one-to-many/many-to-many关系映射<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>class</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
="ClassName" (3)
[hibernate]many-to-one/one-to-many/many-to-many关系映射<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>cascade</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
="all|none|save-update|delete" (4)
[hibernate]many-to-one/one-to-many/many-to-many关系映射<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>outer-join</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
="true|false|auto" (5)
[hibernate]many-to-one/one-to-many/many-to-many关系映射<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>update</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
="true|false" (6)
[hibernate]many-to-one/one-to-many/many-to-many关系映射<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>insert</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
="true|false" (7)
[hibernate]many-to-one/one-to-many/many-to-many关系映射<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>property-ref</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
="propertyNameFromAssociat<wbr>edClass"</wbr> <wbr>(8)<br style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px"><img src="https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif" align="top" alt="[hibernate]many-to-one/one-to-many/many-to-many关系映射" title="[hibernate]many-to-one/one-to-many/many-to-many关系映射" style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; list-style-type:none; list-style-position:initial; border-style:initial; border-color:initial; border-style:initial; border-color:initial"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>access</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> ="field|property|ClassName" (9)
[hibernate]many-to-one/one-to-many/many-to-many关系映射<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>unique</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
="true|false" <wbr>(10)<br style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px"><img src="https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif" align="top" alt="[hibernate]many-to-one/one-to-many/many-to-many关系映射" title="[hibernate]many-to-one/one-to-many/many-to-many关系映射" style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; list-style-type:none; list-style-position:initial; border-style:initial; border-color:initial; border-style:initial; border-color:initial"></wbr> />

1.name:属性名。指出many一方的类用哪个属性和one一方的类关联.
2.column:字段名(可选).指出many一方的类对应的数据表用哪个列和one一方的类对应的数据表关联(两表之间存在外键关联);
3.class:关联的类的名字(可选 - 默认是通过反射得到属性类型);
4.cascade:指明哪些操作会从父对象级联到关联的对象(可选).cascade属性允许下列值::<wbr><code style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px">all</code>,<wbr><code style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px">save-update</code>,<wbr><code style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px">delete</code>,<wbr><code style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px">none</code>. 设置除了<code style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px">none</code>以外的其它值会传播特定的操作到关联的(子)对象中。<br style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px"> 5.outer-join:当设置<code style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px">hibernate.use_outer_join</code>的时候,对这个关联允许外连接抓取(可选 - 默认为<wbr><code style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px">auto</code>).<span style="font-family:Courier New; word-wrap:normal; word-break:normal; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px">outer-join</span>参数允许下列三个不同值: auto(使用外连接抓取关联(对象),如果被关联的对象没有代理(proxy) ),true(一直使用外连接来抓取关联),false(永远不使用外连接来抓取关联);<br style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px"> 6.update,insert:指定对应的字段是否在用于<code style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px">UPDATE</code><wbr>和/或<wbr><code style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px">INSERT</code>的SQL语句中包含。如果二者都是<code style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px">false</code>,则这是一个纯粹的“外源性(derived)”关联,它的值是通过映射到同一个(或多个)字段的某些其他属性得到的,或者通过trigger(除法器),或者是其他程序(可选 - 默认为<wbr><code style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px">true</code>)<br style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px"> 7.property-ref:指定关联类的一个属性,这个属性将会和本外键相对应。如果没有指定,会使用对方关联类的主键(可选).property-ref属性只应该用来对付老旧的数据库系统,可能出现外键指向对方关联表的是个非主键字段(但是应该是一个惟一关键字)的情况。这是一种十分丑陋的关系模型。比如说,假设Product类有一个惟一的序列号,它并不是主键;<br style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px"> 8.access:NHibernate 用来访问属性的策略(可选 - 默认为<code style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px">property</code>)<br style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px"> 9.unique:允许产生外键列唯一约束的数据库定义语言(DDL)(可选)</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值