NHibernate2.1实例使用教程之entity-name特性介绍
NHibernate2.1另外的一个新特性实体名称(entity-name)。 实体名称(entity-name)在Class的Mapping中使用,一般而言,我们并不特意定义它,只有在其Class的Name的属性有点复杂的时候使用一个别名。在
NHibernate2.1另外的一个新特性——实体名称(entity-name)。
实体名称(entity-name)在Class的Mapping中使用,一般而言,我们并不特意定义它,只有在其Class的Name的属性有点复杂的时候使用一个别名。在保存Domain的时候,ISession.Save()也有重载方法。
典型实例
这个实例使用继承映射,对于子类的名称比较复杂,我们可以使用entity-name来重新定义它的名称。
1.Domain
Code [http://www.xueit.com]
public abstract class Animal { public virtual int Id { get; private set; } public virtual string Description { get; set; } } public class Reptile : Animal { public virtual float BodyTemperature { get; set; } } public class Human : Animal { public virtual string Name { get; set; } public virtual string NickName { get; set; } public virtual DateTime Birthdate { get; set; } } public class Family<T> where T : Animal { public virtual int Id { get; private set; } public virtual T Father { get; set; } public virtual T Mother { get; set; } public virtual ISet<T> Childs { get; set; } }
2.Mapping
在数据库中我想每个Animal使用不同的表,所以需要三个不同的表。当然,所有"Kinds"的家庭只有一个表可能不够,因为我不可能有一个ForeignKey指向两个表。我需要一个表有强类型Family。使用NHibernate新的标签:实体名称(entity-name)可以做到。
Code [http://www.xueit.com]
<class name="Animal"> <id name="Id" column="animalId"> <generator class="hilo"/> </id> <property name="Description"/> <joined-subclass name="Reptile"> <key column="animalId"/> <property name="BodyTemperature"/> </joined-subclass> <joined-subclass name="Human"> <key column="animalId"/> <property name="Name"/> <property name="NickName"/> <property name="Birthdate" type="Date"/> </joined-subclass> </class> <class name="Family`1[[Reptile]]" table="ReptilesFamilies" entity-name="ReptilesFamily"> <id name="Id" column="familyId"> <generator class="hilo"/> </id> <many-to-one name="Father" class="Reptile" cascade="all"/> <many-to-one name="Mother" class="Reptile" cascade="all"/> <set name="Childs" generic="true" cascade="all"> <key column="familyId" /> <one-to-many class="Reptile"/> </set> </class> <class name="Family`1[[Human]]" table="HumanFamilies" entity-name="HumanFamily"> <id name="Id" column="familyId"> <generator class="hilo"/> </id> <many-to-one name="Father" class="Human" cascade="all"/> <many-to-one name="Mother" class="Human" cascade="all"/> <set name="Childs" generic="true" cascade="all"> <key column="familyId" /> <one-to-many class="Human"/> </set> </class>
从映射可以看出,一个类实现所有类型家庭,但使用两个不同的强类型持久化映射。
精彩图集
精彩文章
热门标签
c语言知识点
location.hre
阻止冒泡
ProgressDial
java裁剪图片
一些用法
符号
站的
仿dedecms
Python异常处理
传递数据
dfs
position:fix
隐藏控件
Windows
常用方法
nextSibling
memcache扩展
自增号
硬盘使用
无参函数
阿里云
消息窗口
sink
Replica
链接类
选择法
竞争
将
c异或运算
屏幕锁定
金融
urldecode
ExpandableLi
所有进程
标准尺寸
连续元素
本周
语句练习
全文检索
greenlet
更新数据
新手入门
判断上传文件
路由失效
去除空格
swapoff
运行PHP
子序列
window.locat
指向
投影机
cmd窗口
实际
判定
simple_html_
文件 文件删除
format
创建图标
验证图片
赞助商链接
@CopyRight 2002-2008, 1SOHU.COM, Inc. All Rights Reserved QQ:1010969229

