标签:style blog http color io os ar strong 文件
上一篇博客中提到的是单向关联: Nhibernate一对多映射——单向关联。这篇说说双向关联。
双向关联和单向关联的区别是:两边都能维护关系,如我查询两边的任何一边,另外一边的信息也能查询出来,其他的修改删除只要设置了,也都可以。体现在代码中是:因为上篇单向关联是在DictionaryEntity上,所以变为双向关联要在DictionTypeEntity和他对应的xml文件中加上关联映射。
DictionaryEntity修改为:
#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; }
/// <summary>
/// 字典实体list集合
/// </summary>
public virtual IList<DictionaryEntity> DictionaryEntitys { get; set; }
#endregion DictionaryEntity对应的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="DictionaryType" type="String" unsaved-value="null">
<column name="DictionaryType" 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="DateTimeStamp" length="20" not-null="true"/>
</property>
<property name="AddUser" type="String">
<column name="AddUser" length="20" not-null="false"/>
</property>
<!-- 字典实体 -->
<bag name="DictionaryEntitys" inverse="true" cascade="all">
<key column="DictionaryType"/>
<one-to-many class="Model.Entity.DictionaryEntity"/>
</bag>
</class>
</hibernate-mapping>
此时,如果我们查询了DictionaryEntity的信息,想知道DictionaryTypeEntity的Value的信息,只要在DictionaryEntity.DictionaryType.Value就能查询出来我们想要的值。如果我们想保存DictionaryEntity的信
息,我们得先实例化一个DictionaryTypeEntity,并赋值给DictionaryEntity.DictionaryType。如下:
DictionaryTypeEntity enDictionaryType=new DictionaryTypeEntity(); DictionaryEntity enDictionary=new DictionaryEntity(); enDictionary.DictionaryType=enDictionaryType;
标签:style blog http color io os ar strong 文件
原文地址:http://blog.csdn.net/zuozuo1245/article/details/39235001