一:新闻信息的表已经建立,并能顺利显示出来后,我们要对新闻信息进行分类,因此,我们现在来添加一个表:FS_PT_Class_Tree
ALTER TABLE [dbo].[FS_PT_Announcement] DROP CONSTRAINT FK_FS_PT_Announcement_FS_PT_Class_Tree
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[FS_PT_Class_Tree]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[FS_PT_Class_Tree]
GO
CREATE TABLE [dbo].[FS_PT_Class_Tree] (
[ClassID] [int] NOT NULL ,
[ClassName] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[ClassUrl] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NULL ,
[Priority] [int] NOT NULL ,
[ParentID] [int] NOT NULL ,
[IsActive] [int] NOT NULL
) ON [PRIMARY]
GO
PT_Class_Tree:公告信息类别树 | ||
ClassID | int | 类别标识 |
ClassName | varchar | 类别名称 |
ClassUrl | varchar | 类别链接 |
Priority | int | 排序的优先级 |
ParentID | int | 上级标识 |
IsActive | int | 激活 |
二:使用学习笔记二中的方法,生成该表的Model和hbm文件。代码如下:(Class_Tree.cs文件代码)
using System.Data;
using System.Collections.Generic;
using System.Text;
using FastSpring.Base.Model;
using FastSpring.Core.Model;
using FastSpring.Common;
using System.Collections;
namespace DZ_Portal.App.Model
...{
/**//// <summary>
///
/// </summary>
[Serializable]
public sealed class Class_Tree: BaseVersionModel
...{
私有成员#region 私有成员
private bool m_IsChanged;
private bool m_IsDeleted;
private int m_ClassID;
private string m_ClassName;
private string m_ClassUrl;
private int m_Priority;
private int m_ParentID;
private int m_IsActive;
//一对多关系:
private IList m_Announcement;
#endregion
GetEntityType()#region GetEntityType()
public override Type GetEntityType()
...{
return typeof(Class_Tree);
}
#endregion
public override bool EqualsID(int ID)
...{
return this.ClassID == ID;
}
默认( 空 ) 构造函数#region 默认( 空 ) 构造函数
/**//// <summary>
/// 默认构造函数
/// </summary>
public Class_Tree()
...{
m_ClassID = 0;
m_ClassName = null;
m_ClassUrl = null;
m_Priority = 0;
m_ParentID = 0;
m_IsActive = 0;
m_Announcement = new ArrayList();
}
#endregion
公有属性#region 公有属性
/**//// <summary>
///
/// </summary>
public int ClassID
...{
get ...{ return m_ClassID; }
set ...{ m_IsChanged |= (m_ClassID != value); m_ClassID = value; }
}
/**//// <summary>
///
/// </summary>
public string ClassName
...{
get ...{ return m_ClassName; }
set
...{
if (value != null)
if (value.Length > 50)
throw new ArgumentOutOfRangeException("Invalid value for ClassName", value, value.ToString());
m_IsChanged |= (m_ClassName != value); m_ClassName = value;
}
}
/**//// <summary>
///
/// </summary>
public string ClassUrl
...{
get ...{ return m_ClassUrl; }
set
...{
if (value != null)
if (value.Length > 100)
throw new ArgumentOutOfRangeException("Invalid value for ClassUrl", value, value.ToString());
m_IsChanged |= (m_ClassUrl != value); m_ClassUrl = value;
}
}
/**//// <summary>
///
/// </summary>
public int Priority
...{
get ...{ return m_Priority; }
set ...{ m_IsChanged |= (m_Priority != value); m_Priority = value; }
}
/**//// <summary>
///
/// </summary>
public int ParentID
...{
get ...{ return m_ParentID; }
set ...{ m_IsChanged |= (m_ParentID != value); m_ParentID = value; }
}
/**//// <summary>
///
/// </summary>
public int IsActive
...{
get ...{ return m_IsActive; }
set ...{ m_IsChanged |= (m_IsActive != value); m_IsActive = value; }
}
/**//// <summary>
/// 对象的值是否被改变
/// </summary>
public bool IsChanged
...{
get ...{ return m_IsChanged; }
}
/**//// <summary>
/// 对象是否已经被删除
/// </summary>
public bool IsDeleted
...{
get ...{ return m_IsDeleted; }
}
/**//// <summary>
/// 对象中的所有新闻列表
/// </summary>
public IList Announcements
...{
get ...{ return m_Announcement; }
set
...{
m_IsChanged |= (m_Announcement != value);
m_Announcement = value;
}
}
#endregion
公有函数#region 公有函数
/**//// <summary>
/// 标记对象已删除
/// </summary>
public void MarkAsDeleted()
...{
m_IsDeleted = true;
m_IsChanged = true;
}
#endregion
重写Equals和HashCode#region 重写Equals和HashCode
/**//// <summary>
/// 用唯一值实现Equals
/// </summary>
public override bool Equals(object obj)
...{
if (this == obj) return true;
if ((obj == null) || (obj.GetType() != GetType())) return false;
Class_Tree castObj = (Class_Tree)obj;
return (castObj != null) &&
(m_ClassID == castObj.ClassID);
}
/**//// <summary>
/// 用唯一值实现GetHashCode
/// </summary>
public override int GetHashCode()
...{
int hash = 57;
hash = 27 * hash * m_ClassID.GetHashCode();
return hash;
}
#endregion
}
}
注意:这里有个定义:private IList m_Announcement;(该对象是用来设置一对多关联用的,在代码中所有涉及到m_Announcement的都是和Announcement有关系的,它是该类下的所有新闻信息列表,生成工具中生成的时候没有这个的代码的。)下面的是Class_Tree.hbm.xml代码:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="DZ_Portal.App.Model.Class_Tree,DZ_Portal.App" table="PT_Class_Tree" lazy="false">
<id name="ClassID" column="ClassID" type="Int32" unsaved-value="0">
<generator class="assigned"/>
</id>
<property column="ClassName" type="String" name="ClassName" not-null="true" length="50" />
<property column="ClassUrl" type="String" name="ClassUrl" length="100" />
<property column="Priority" type="Int32" name="Priority" not-null="true" />
<property column="ParentID" type="Int32" name="ParentID" not-null="true" />
<property column="IsActive" type="Int32" name="IsActive" not-null="true" />
<bag name="Announcements" cascade="all" inverse="true" lazy="true">
<key column="ClassID"/>
<one-to-many class="DZ_Portal.App.Model.Announcement,DZ_Portal.App"/>
</bag>
</class>
</hibernate-mapping>
bag 节点的也是配置关联用的,该对象的生成是否有问题现在还不知道,目前没有着手创建出来测试。后面会做测试的。现在主要是为了Announcement对象关联时用到Class_Tree对象而用的。
三、本来要继续加入BLL层和DAL层的代码,发现我这里都没有这两层的代码,但是程序一样没有出错,那么我们看看Announcement对象该怎么改:下面是Announcement.cs改后的代码:
using System.Data;
using System.Collections.Generic;
using System.Text;
using FastSpring.Base.Model;
using FastSpring.Core.Model;
using FastSpring.Common;
namespace DZ_Portal.App.Model
...{
/**//// <summary>
///
/// </summary>
[Serializable]
public sealed class Announcement: BaseVersionModel
...{
私有成员#region 私有成员
private bool m_IsChanged;
private bool m_IsDeleted;
private int m_ItemID;
private int m_ModuleID;
private string m_CreateByUser;
private DateTime m_CreatedDate;
private string m_Title;
private string m_MoreLink;
private DateTime m_ExpireDate;
private string m_Description;
private int m_Frequency;
private int m_IsActive;
private int m_Priority;
private int m_ClassID;
//一对一关系
private Class_Tree m_Class_Tree;
#endregion
GetEntityType()#region GetEntityType()
public override Type GetEntityType()
...{
return typeof(Announcement);
}
#endregion
默认( 空 ) 构造函数#region 默认( 空 ) 构造函数
/**//// <summary>
/// 默认构造函数
/// </summary>
public Announcement()
...{
m_ItemID = 0;
m_ModuleID = 0;
m_CreateByUser = null;
m_CreatedDate = DateTime.MinValue;
m_Title = null;
m_MoreLink = null;
m_ExpireDate = DateTime.MinValue;
m_Description = null;
m_Frequency = 0;
m_IsActive = 0;
m_Priority = 0;
m_ClassID = 0;
m_Class_Tree=null;
}
#endregion
公有属性#region 公有属性
/**//// <summary>
///
/// </summary>
public Class_Tree class_tree
...{
get ...{ return m_Class_Tree; }
set ...{ m_IsChanged |= (m_Class_Tree != value); m_Class_Tree = value; }
}
/**//// <summary>
///
/// </summary>
public int ItemID
...{
get ...{ return m_ItemID; }
set ...{ m_IsChanged |= (m_ItemID != value); m_ItemID = value; }
}
/**//// <summary>
///
/// </summary>
public int ModuleID
...{
get ...{ return m_ModuleID; }
set ...{ m_IsChanged |= (m_ModuleID != value); m_ModuleID = value; }
}
/**//// <summary>
///
/// </summary>
public string CreateByUser
...{
get ...{ return m_CreateByUser; }
set
...{
if (value != null)
if (value.Length > 50)
throw new ArgumentOutOfRangeException("Invalid value for CreateByUser", value, value.ToString());
m_IsChanged |= (m_CreateByUser != value); m_CreateByUser = value;
}
}
/**//// <summary>
///
/// </summary>
public DateTime CreatedDate
...{
get ...{ return m_CreatedDate; }
set ...{ m_IsChanged |= (m_CreatedDate != value); m_CreatedDate = value; }
}
/**//// <summary>
///
/// </summary>
public string Title
...{
get ...{ return m_Title; }
set
...{
if (value != null)
if (value.Length > 100)
throw new ArgumentOutOfRangeException("Invalid value for Title", value, value.ToString());
m_IsChanged |= (m_Title != value); m_Title = value;
}
}
/**//// <summary>
///
/// </summary>
public string MoreLink
...{
get ...{ return m_MoreLink; }
set
...{
if (value != null)
if (value.Length > 150)
throw new ArgumentOutOfRangeException("Invalid value for MoreLink", value, value.ToString());
m_IsChanged |= (m_MoreLink != value); m_MoreLink = value;
}
}
/**//// <summary>
///
/// </summary>
public DateTime ExpireDate
...{
get ...{ return m_ExpireDate; }
set ...{ m_IsChanged |= (m_ExpireDate != value); m_ExpireDate = value; }
}
/**//// <summary>
///
/// </summary>
public string Description
...{
get ...{ return m_Description; }
set
...{
if (value != null)
if (value.Length > 2147483647)
throw new ArgumentOutOfRangeException("Invalid value for Description", value, value.ToString());
m_IsChanged |= (m_Description != value); m_Description = value;
}
}
/**//// <summary>
///
/// </summary>
public int Frequency
...{
get ...{ return m_Frequency; }
set ...{ m_IsChanged |= (m_Frequency != value); m_Frequency = value; }
}
/**//// <summary>
///
/// </summary>
public int IsActive
...{
get ...{ return m_IsActive; }
set ...{ m_IsChanged |= (m_IsActive != value); m_IsActive = value; }
}
/**//// <summary>
///
/// </summary>
public int Priority
...{
get ...{ return m_Priority; }
set ...{ m_IsChanged |= (m_Priority != value); m_Priority = value; }
}
/**//// <summary>
///
/// </summary>
public int ClassID
...{
get ...{ return m_ClassID; }
set ...{ m_IsChanged |= (m_ClassID != value); m_ClassID = value; }
}
/**//// <summary>
/// 对象的值是否被改变
/// </summary>
public bool IsChanged
...{
get ...{ return m_IsChanged; }
}
/**//// <summary>
/// 对象是否已经被删除
/// </summary>
public bool IsDeleted
...{
get ...{ return m_IsDeleted; }
}
#endregion
公有函数#region 公有函数
/**//// <summary>
/// 标记对象已删除
/// </summary>
public void MarkAsDeleted()
...{
m_IsDeleted = true;
m_IsChanged = true;
}
#endregion
重写Equals和HashCode#region 重写Equals和HashCode
/**//// <summary>
/// 用唯一值实现Equals
/// </summary>
public override bool Equals(object obj)
...{
if (this == obj) return true;
if ((obj == null) || (obj.GetType() != GetType())) return false;
Announcement castObj = (Announcement)obj;
return (castObj != null) &&
(m_ItemID == castObj.ItemID);
}
/**//// <summary>
/// 用唯一值实现GetHashCode
/// </summary>
public override int GetHashCode()
...{
int hash = 57;
hash = 27 * hash * m_ItemID.GetHashCode();
return hash;
}
#endregion
public override bool EqualsID(int ID)
...{
return this.ItemID == ID;
}
}
}
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="DZ_Portal.App.Model.Announcement,DZ_Portal.App" table="PT_Announcement" lazy="false">
<id name="ItemID" column="ItemID" type="Int32" unsaved-value="0">
<generator class="native"/>
</id>
<property column="ModuleID" type="Int32" name="ModuleID" />
<property column="CreateByUser" type="String" name="CreateByUser" not-null="true" length="50" />
<property column="CreatedDate" type="DateTime" name="CreatedDate" not-null="true" />
<property column="Title" type="String" name="Title" not-null="true" length="100" />
<property column="MoreLink" type="String" name="MoreLink" not-null="true" length="150" />
<property column="ExpireDate" type="DateTime" name="ExpireDate" not-null="true" />
<property column="Description" type="String" name="Description" not-null="true" length="2147483647" />
<property column="Frequency" type="Int32" name="Frequency" not-null="true" />
<property column="IsActive" type="Int32" name="IsActive" not-null="true" />
<property column="Priority" type="Int32" name="Priority" not-null="true" />
<property column="ClassID" type="Int32" name="ClassID" not-null="true" />
<many-to-one name="class_tree" class="DZ_Portal.App.Model.Class_Tree,DZ_Portal.App" column="ClassID" unique="true"></many-to-one>
</class>
<query name="GetAnnouncementsOfClassID">
<![CDATA[from Announcement announcement WHERE announcement.IsActive=1 AND announcement.ClassID=:param1 ORDER BY announcement.CreatedDate DESC,announcement.Priority DESC]]>
</query>
</hibernate-mapping>
<!--1。HBM映射配置文件中“urn:nhibernate-mapping-2.0 ”需改为“urn:nhibernate-mapping-2.2”
2。 <class name="类名,程序集名" table="Archives" >需改为<class name="类名,程序集名
" table="Archives" lazy="false">,需要添加“lazy="false"”,不然会出现错误:
he following types may not be used as proxies: xxxxx: method get_CreateDate should be virtual
3。Spring.Data.NHibernate程序集变更为:Spring.Data.NHibernate12 (注意:Spring.Data.NHibernate
程序集仍然存在,是对NHibernate1.0X的支持,Spring.Data.NHibernate12 为新增的支持NHibernate1.2.0GA的程序集)
-->
many-to-one是设置关联是加进去的,因为很多条新闻对应1个类对象,也可以1个新闻对应1个类对象,所以这里是设置many-to-one。
解说:由于业务实体之间的关联不涉及到其它层次,因此这里Class_Tree中可以没有DAL和BLL层,nhibernate的实体关联只涉及到实体与实体之间的关系,只要你配置好它的hbm文件,实体类代码中使用到相关联的对象,那么,它在实体生成时会根据配置文件自动处理相关联的对象的。下面我们来看看关联后的结果。我们在Index页面中有一处是显示新闻信息的搜索结果的,我们当时显示的是Class_ID的,那么我们现在要显示Class_ID对应的类名称:于是我们可以把“<%...#DataBinder.Eval(Container.DataItem, "ClassID")%></a>”这段改成:“<%#DataBinder.Eval(Container.DataItem, "Class_Tree.ClassName")%></a>”(我都不敢相信,这样改也能正常显示出来,什么原理?知道的朋友麻烦告诉我一下)运行程序就可以看到修改后的结果了!
原创作品出自努力偷懒,转载请说明文章出处:http://blog.csdn.net/kfarvid或 http://www.cnblogs.com/kfarvid/