Nhibernate初入门基本配置(二) 一、NHibernate基本配置 二、获取映射文件相关配置 “/”应用程序中的服务器错误。

转载地址http://www.cnblogs.com/kissdodog/p/3306428.html

 使用NHibernate最重要的一步就是配置,如果连NHibernate都还没有跑的起来,谈何学习。今天就来详解一下NHibernate的配置。

  NHibernate配置要注意的有:

  1、NHibernate需要一个自定义的配置节点,一般放在Web.config里或App.config里面,当然你可以自己定义实际位置。

  示例如下:

  

<configSections>
    <section name="hibernate-configuration"
        type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"
        />
  </configSections>
  <!-- Add this element -->
  <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
      <property name='proxyfactory.factory_class'>NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
      <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
      <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
      <property name="connection.connection_string">
        Data Source=.;Initial Catalog=Zhijiao;Integrated Security=True
      </property>
      <mapping assembly="zhijiao" />
    </session-factory>
  </hibernate-configuration>

实体类

namespace zhijiao
{
    public class NewsPub
    {

        /// <summary>
        /// ID
        /// </summary>
        public virtual int ID
        {
            get;
            set;
        }
        /// <summary>
        /// NTitle
        /// </summary>
        public virtual string NTitle
        {
            get;
            set;
        }
        /// <summary>
        /// NContent
        /// </summary>
        public virtual string NContent
        {
            get;
            set;
        }
        /// <summary>
        /// NDate
        /// </summary>
        public virtual string NDate
        {
            get;
            set;
        }
        /// <summary>
        /// NHits
        /// </summary>
        public virtual int NHits
        {
            get;
            set;
        }
        /// <summary>
        /// NType
        /// </summary>
        public virtual int NType
        {
            get;
            set;
        }
        /// <summary>
        /// NPic
        /// </summary>
        public virtual string NPic
        {
            get;
            set;
        }

    }
}

 

实体类XML

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
    namespace="zhijiao" assembly="zhijiao">

  <class name="zhijiao.NewsPub,zhijiao" table="NewsPub">

    <!-- A 32 hex character is our surrogate key. It's automatically
            generated by NHibernate with the UUID pattern. -->
    <id name="ID" type="Int32">
      <column name="ID"   not-null="true"/>
      <generator class="identity" />
    </id>

    <!-- A cat has to have a name, but it shouldn' be too long. -->
    <property name="NTitle" type="string">
      <column name="NTitle"  length="50" not-null="true" />
    </property>
    <property name="NContent" type="string">
      <column name="NContent"   not-null="true" />
    </property>
    <property name="NDate" type="StringClob">
      <column name="NDate" length="2147483647"   not-null="true" />
    </property>
    <property name="NHits" type="Int32">
      <column name="NHits"   not-null="true" />
    </property>
    <property name="NType" type="Int32">
      <column name="NType"   not-null="true" />
    </property>
    <property name="NPic" type="string">
      <column name="NPic" length="50"   not-null="true" />
    </property>
  </class>

</hibernate-mapping>

加载Nhibernate  

 private static readonly ISessionFactory sessionFactory = new Configuration().Configure().BuildSessionFactory();

sessionFactory.OpenSession();

  另外,这一个配置节点文件,并不一定要放在Web.config或App.config里,例如你可以定义一个hibernate.cfg.xml文件,然后创建ISessionFactory时指定位置:

ISessionFactory sessionFactory = new Configuration().Configure(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "hibernate.cfg.xml").BuildSessionFactory();

  这里要小心"new Configuration().Configure()",如果没有Configure("xxx.cfg.xml")就是从配置文件加载,有就是从自定义文件(如:xxx.cfg.xml)加载。

  注意,此hibernate.cfg.xml文件名可以随意起,但是要设置为始终复制。否则将报告如下错误:

  An exception occurred during configuration of persistence layer.

   2、NHibernate的映射文件一定要设为嵌入的资源。

  Nhibernate初入门基本配置(二)
一、NHibernate基本配置
二、获取映射文件相关配置
“/”应用程序中的服务器错误。

  3、NHibernate的实体类所有的属性都要加上virtual-虚属性,具体将在持久化类的文章中说明,下面给个例子:

    public class PersonModel
    {
        public virtual int Id { get; set; }

        public virtual string Name { get; set; }
    }

   NHibernate作为一个ORM框架,它需要知道如何将数据库的表与对应的实体类相关联,NHibernate通过映射文件的方式来获得这方面的信息。

二、获取映射文件相关配置

  NHibernate中的映射文件以hbm.xml为后缀结束,NHibernate获取映射文件的方法有3种方法。

  1、从config程序配置文件获取映射文件位置

  NHibernate运行在.Net程序上,需要自己提供一个自定义配置节点,这个配置节点里面包含了数据库连接,什么数据库等等信息。其中就包括了配置文件所在位置的信息。

  NHibernate会直接从Web.config或App.config文件上的配置节点上读取这个节点,然后从节点指定的程序集去查找映射文件。

  <mapping assembly ="Nx.Repository"/>  

   以上这个配置节点所保存的就是映射文件默认所在的程序集。这样当程序启动时,NHibernate就会自动去Nx.Repository寻找*.hbm.xml映射文件。

  如果NHibernate找不到映射文件,则可能会报如下错误:

“/”应用程序中的服务器错误。


No persister for: xxx.xxx

 

   2、编程的方式,写在代码里

  (1)、AddFile 指定映射文件

  ISessionFactory sessionFactory = new Configuration().Configure().AddFile(@"C:UsersAdministratorDesktopNHibernate学习ModelMappingPerson.hbm.xml").BuildSessionFactory();

  注意,当你不写全路径的时候,也就是只写个文件名,那么NHibernate就会从配置文件读取程序集,到配置文件中指定的程序集去加载此映射文件。如果配置文件并不指定程序集,那么就从当前程序集中查找指定名的映射文件。但这种方式,不必设置嵌入的资源,但要记得设置为始终复制。

  Configuration cfg = new Configuration().AddFile("Mapping/Person.hbm.xml");
  ISessionFactory sessionFactory = cfg.BuildSessionFactory();

  这种方法只是指定一个文件,但是可以链式指定多个如:AddFile("f1").AddFile("f2")。

  (2)、AddAssembly

  还有另外一种方法,在创建Configuration时指定程序集。

  Configuration cfg = new Configuration().AddAssembly("Model");       //Model为映射文件所在程序集名称
  ISessionFactory sessionFactory = cfg.BuildSessionFactory();

  这种方法,即使.config配置文件里<mapping assembly=""/>节点为空也没关系,NHibernate会自动到指定的程序集里查找所有以.hbm.xml结尾的文件。

  (3)、AddClass

  这种方式,可以消除程序对文件路径字符串的硬编码。

  Configuration cfg = new Configuration().AddClass(typeof(Model.Person));
  ISessionFactory sessionFactory = cfg.BuildSessionFactory();

  但是缺点也非常明显,类名必须与映射文件的名称相同。不然,编译就无法通过,因为Person类都找不到。另外,映射文件Person.hbm.xml必须放在根目录。

  NHibernate会自动到程序中去查找名为Person.hbm.xml文件。