码迷,mamicode.com
首页 > 系统相关 > 详细

Nhibernate一对多映射

时间:2014-08-30 23:10:10      阅读:289      评论:0      收藏:0      [点我收藏+]

标签:style   blog   使用   io   strong   ar   文件   数据   2014   

在Nhibernate中,映射文件对应数据库表中的关系,所有Nhibernate中也有一对一映射、一对多映射和多对多映射。首先,看看一对多映射,一对多映射就是数据库中两表的关系是一对多的关系,例如:学生和班级的关系,就是一对多的关系,一个班级有多个学生,一个学生只属于一个班级;字典类型和字典表也是一对多的关系。用字典类型和字典表做实例:

一对多关联映射有单向和双向之分,单向表示在一边维护他们的关系,双向表示在两边都要维护关系。首先看下单向一对多关联映射。

字典类型DictionTypeEntity实体和字典实体DictionEntity:

public class DictionaryEntity
	{
        /// <summary>
        /// ID
        /// </summary>
        public virtual Guid ID { get; set; }
        /// <summary>
        /// 键
        /// </summary>
        public virtual string Key { get; set; }
        /// <summary>
        /// 键值
        /// </summary>
		public virtual string Value { get; set; }
        /// <summary>
        /// 类型
        /// </summary>
		public virtual string Type { get; set; }
        /// <summary>
        /// 时间戳
        /// </summary>
        public virtual string TimeStamp { get; set; }
        /// <summary>
        /// 操作用户
        /// </summary>
		public virtual string AddUser { get; set; }
        /// <summary>
        /// 备注,描述
        /// </summary>
        public virtual string Remark { get; set; }

        	
	}
<pre name="code" class="csharp">/// <summary>
    ///  该实体类与数据库表'T_DictionaryManage'.
	/// </summary>
	public class DictionaryTypeEntity
	{
		#region 实体属性
        /// <summary>
        /// 类型
        /// </summary>
        public virtual string Type { get;set; }
        /// <summary>
        /// 类型名称
        /// </summary>
		public virtual string TypeName  { get; set; }
        /// <summary>
        /// 时间戳
        /// </summary>
		public virtual string TimeStamp	{ get;set; }
        /// <summary>
        /// 操作用户
        /// </summary>
		public virtual string AddUser{ get; set; }
		

		#endregion

        /// <summary>
        /// 字典实体list集合
        /// </summary>
        public virtual IList<DictionaryEntity> DictionaryEntitys { get; set; }
	}




字典类型DictionTypeEntity对应的hbm.xml文件:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
	<class name="Model.Entity.DictionaryTypeEntity, Model" table="T_DictionaryManage">
		<id name="Type" type="String" unsaved-value="null">
			<column name="Type" length="20"    unique="true"/>
			<generator class="assigned" />
		</id>
		<property name="TypeName" type="String">
			<column name="TypeName" length="50"   not-null="true"/>
		</property>
		<property name="TimeStamp" type="String">
			<column name="`TimeStamp`" length="20"   not-null="true"/>
		</property>
		<property name="AddUser" type="String">
			<column name="AddUser" length="20"   not-null="false"/>
		</property>
    <span style="white-space:pre">		</span><!-- 字典实体 -->
    <span style="white-space:pre">		</span><bag name="DictionaryEntitys" inverse="true" cascade="all">
      <span style="white-space:pre">			</span><key column="Type"/>
      <span style="white-space:pre">			</span><one-to-many class="Model.Entity.DictionaryEntity"/>
    <span style="white-space:pre">		</span></bag>
	</class>
</hibernate-mapping>


字典实体DictionEntity对应的hbm.xml文件:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Model.Entity.DictionaryEntity, Model" table="T_Dictionary">
    <cache usage="read-write"/>
<id name="ID" type="Guid" >
<column name="ID" length="150"  index="PK_Dictionary"/>
<generator class="guid" />
</id>
<property name="Key" type="String">
<column name="KeyName" length="20"   not-null="false"/>
</property>
<property name="Value" type="String">
<column name="ValueName" length="20"   not-null="false"/>
</property>
<property name="Type" type="String">
<column name="DictionaryType" length="20"   not-null="false"/>
</property>
    <!-- 多的一端,字典类型(去掉双向关联映射)-->
    <!--<many-to-one name="Type"   class="Model.Entity.DictionaryTypeEntity" column="DictionaryType" />-->
<property name="TimeStamp" type="String" length="20">
<column name="DateTimeStamp"   not-null="false"/>
</property>
<property name="AddUser" type="String">
<column name="AddUser" length="20"   not-null="false"/>
</property>
<property name="Remark" type="String">
<column name="Remark" length="50"   not-null="false"/>
</property>
</class>
</hibernate-mapping>



这样查询一个字典类型时,字典表里该类型的所有记录也会查询出来,这样显见的是不用两个表联合查询了。

Nhibernate里支持懒加载,如果我们设置lazy="true",则字典表里对应的记录一开始不会查询出来,只有真正使用的时候才会查出来,这样就可以提高一些效率。

Nhibernate一对多映射

标签:style   blog   使用   io   strong   ar   文件   数据   2014   

原文地址:http://blog.csdn.net/zuozuo1245/article/details/38947729

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!