1、NHibernate入门

    NHibernate是一个成熟的.net O/R(对象/关系数据库)映射工具,但一直没学习和使用它,最近不太忙,静下心来慢慢学习,就直接帮助文档中文帮助文档下载)啦。入门还是挺简单的,下面就说明下如何创建一个简单的示例。

一、创建NHibernate配置

    这个可以在下载的NHibernate文件中的Configuration_Templates文件夹下找到一个相应数据库(有MSSQL.cfg.xml、Oracle.cfg.xml等)的模板,修改名称成hibernate.cfg.xml,添加到你的项目,修改其中一些属性,主要有dialect、connection.connection_string属性。dialect表示使用数据库类型,可在NHibernate.Dialect空间下找到所有的类型。connection.connection_string就是数据库链接串,和一般程序一样。最好将该文件"Copy to Output"属性为"Copy always".(Set its property "Copy to Output" to "Copy always".)下面给出一个使用SQL Server2008的示例:

<hibernate-configuration  xmlns="urn:nhibernate-configuration-2.2">
<session-factory name="NHBExample">
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">
Data Source=JAY-PC\SQLEXPRESS2008;Initial Catalog=Study;User Id=sss;Password=123456;
</property>
<property name="adonet.batch_size">10</property>
<property name="show_sql">false</property>
<property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
<property name="command_timeout">60</property>
<property name="query.substitutions">1</property>
<property name="show_sql">True</property>
<mapping assembly="NHBExample"/>
</session-factory>
</hibernate-configuration>
    <mapping assembly="NHBExample" />这句很关键啊,配置了此属性NHibernate会自动在该程序集(QuickStart修改成你自己的程序集)下自动查找.hbm.xml文件,与对应的持久类建立映射。
二、创建持久类及其O/R映射文件
    创建持久化类StudentEntity,属性修饰符添加virtual。映射文件Student.hbm.xml定义了持久类映射的表、持久类属性映射的字段(程序集、命名空间也要配置好),修改文件属性,改为嵌入资源。如:
View Code
namespace NHBExample.Model
{
public class StudentEntity
{
public virtual int No
{
get;
set;
}

public virtual string Name
{
get;
set;
}
}
}
//Student.hbm.xml
View Code
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHBExample" namespace="NHBExample.Model">
<class name="StudentEntity" table="Student">
<id name="No" column="Student_ID">
<generator class="native"></generator>
</id>
<property name="Name" column="Name"/>
</class>
</hibernate-mapping>

三、操作数据

    这部操作步骤如下:

 1、通过对Configuration().Configure()的调用来装载配置文件,并初始化成一个Configuration实例,通过Configuration实例创建一个ISessionFactory
 2、从ISessionFactory中获取一个ISession(NHibernate的工作单元)
 3、数据操作(增删改查)
 4、关闭ISession

示例:

Configuration configuration = new Configuration( ).Configure( );
ISessionFactory sessionFactory = configuration.BuildSessionFactory( );
ISession session = sessionFactory.OpenSession( );
StudentEntity studentEntity = session.Get<StudentEntity>( id );
session.Close();

  由于ISessionFactory通常只是被初始化一次,比如说在Application_Start事件里来初始化。 这意味着你不应该在ASP.NET页面中把它作为一个实例变量来持有,而应该放在其他地方。进一步的说, 我们需要使用单例(Singleton)模式,我们才能更容易的在程序中访问 ISessionFactory。NHibernate帮助文档已给了一个解决方案。(http://nhforge.org/doc/nh/en/index.html#quickstart-playingwithcats
 
View Code
 1     public sealed class NHibernateHelper
2 {
3 private const string CurrentSessionKey = "nhibernate.current_session";
4 private static readonly ISessionFactory sessionFactory;
5
6 static NHibernateHelper()
7 {
8 sessionFactory = new Configuration().Configure().BuildSessionFactory();
9 }
10
11 public static ISession GetCurrentSession()
12 {
13 HttpContext context = HttpContext.Current;
14 ISession currentSession = context.Items[CurrentSessionKey] as ISession;
15
16 if (currentSession == null)
17 {
18 currentSession = sessionFactory.OpenSession();
19 context.Items[CurrentSessionKey] = currentSession;
20 }
21
22 return currentSession;
23 }
24
25 public static void CloseSession()
26 {
27 HttpContext context = HttpContext.Current;
28 ISession currentSession = context.Items[CurrentSessionKey] as ISession;
29
30 if (currentSession == null)
31 {
32 // No current session
33 return;
34 }
35
36 currentSession.Close();
37 context.Items.Remove(CurrentSessionKey);
38 }
39
40 public static void CloseSessionFactory()
41 {
42 if (sessionFactory != null)
43 {
44 sessionFactory.Close();
45 }
46 }
47 }
48

上述示例变成:
ISession session = NHibernateHelper.GetCurrentSession( );
StudentEntity studentEntity = session.Get<StudentEntity>( catID );
 
上面就是一个简单的NHibernate示例,NHibernate配置、O/R Mapping文件等的细节还得继续学习!
posted @ 2011-11-22 09:59  紫竹山庄  阅读(414)  评论(0编辑  收藏  举报