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

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)可以做到。

<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>
从映射可以看出,一个类实现所有类型家庭,但使用两个不同的强类型持久化映射。
精彩图集
精彩文章
热门标签
类字符串
js调用后台
基础 数组去重
监听机制
点击消失
函数模板
MySQL命令行
复合查询
绝对
php php中通过
具名函数
length
restore
Replica
长按
bz2
Nagios
运行原理
高性能优化
表格行顺序
mysql5.6
磁盘读写
ED2K
mysql小技巧
dim函数
input标签
thinkphp3
TINYINT
HTMLElement
session赋值
级别
Oracle_updat
gif
非对称密钥
灾难性故障
DNS
作用
cli换行
MSDE
取地址
希尔排序
Shell.Applic
services
表数据
约瑟夫
提示模块不存
单条评论
汇总
数据
读取html
双网卡
webFrom
新浪api接口
c语言可变参数
查看字符集
INSER
wince
克隆管理员
CSS居中
Request.Form
赞助商链接
@CopyRight 2002-2008, 1SOHU.COM, Inc. All Rights Reserved QQ:1010969229