Nhibernate使用的一些注意的地方

  

配置文件
hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration  xmlns="urn:nhibernate-configuration-2.0" >
 <session-factory name="NHibernate.Test">
  <!-- properties -->
  <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
  <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
  <property name="connection.connection_string">Server=localhost;initial catalog=nhibernate;Integrated Security=SSPI</property>
  <property name="show_sql">false</property>
  <property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>
  <property name="use_outer_join">true</property>
  <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
  <!-- mapping files -->
  <mapping assembly="NHibernate.Examples" />
 </session-factory>
 </hibernate-configuration>

配置还可以写在.config文件中
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <configSections>
  <section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.3300.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />
 </configSections>
 <nhibernate>
  <add key="hibernate.connection.provider"         
   value="NHibernate.Connection.DriverConnectionProvider"
  />

  <add key="hibernate.dialect"                     
   value="NHibernate.Dialect.MsSql2000Dialect"
  />

  <add key="hibernate.connection.driver_class"         
   value="NHibernate.Driver.SqlClientDriver"
  />

  <add key="hibernate.connection.connection_string"
   value="Server=localhost;initial catalog=test;user=sa;password="
  />
 </nhibernate>
</configuration>

实体映射
Nhibernate可以自动建表/关系吗,好像不行哦。唉,先手工建表吧,这先R还是先O已经是众说纷纭了。

实体类没什么好说的。
XML文件的tag class有些奇怪。如
<class name="guestbook.data.users, guestbook.data" table="G_users">
其中guestbook.data 这是一个assembly,是project的名称。当时伤脑筋了很久,概念模糊。

运行
   Configuration cfg=new Configuration();
   //加载XML
   cfg.AddXmlFile("users.hbm.xml");
   // … …
//加载其它XML
   
   //另一种加载方式。直接加载类,相同效果
 // cfg.AddClass( typeof(guestbook.data.users) )

   // Nhibernate推荐。将guestbook.data下后缀hbm.xml的映射文件全部装载
   // .AddAssembly( " guestbook.data " );

   ISessionFactory f=cfg.BuildSessionFactory();
   ISession s=f.OpenSession();
   ITransaction t=s.BeginTransaction();

   users newUser=new users();
   //users的属性操作
   s.Save(newUser);

   t.Commit();
   s.Close();

运行中碰到的问题
1、 因为调试,DLL生成路径在bin\debug。我的XML一直找不到,有帖子说XML作为附加资源问题就解决了。试过还是不行,就手工复制到bin\debug了。:) 笨办法
2、 写了ASP.net的页面,又连不到数据库了。后来才发觉web.config中hibernate.connection.connection_string的值不能是Server=localhost;initial catalog=guestbook;Integrated Security=SSPI,不指定sa&密码,ASP.net怎么连得上                                    

//Configuration cfg = new Configuration().Configure(@"D:\libRemotingNhibernate\Core\Domain\hibernate.cfg.xml");

/////搞了半天YTForever.Core.Domain 是个程序集,原来以为是个命名空间,郁闷.
  <class name="YTForever.Core.Domain.Message,YTForever.Core.Domain" >


  /////////////////////必选加载程序集,而不是命名空间
    cfg.AddAssembly(@"libRemotingNhibernate");
    cfg.AddAssembly(Assembly.GetExecutingAssembly());
    factory = cfg.BuildSessionFactory();
    ISession session = factory.OpenSession();


///////////程序集要定好,否则后来改要改的地方比较多. 需要鼠标右键在属性里修改.然后改每个xml文件.


            SessionFactory sf = SessionFactory.GetInstance();
            //////这样可以随时注册一个类,比较好.
            sf.RegisterPersistentClass(typeof(YTForever.Core.Domain.Message));
            sf.Rebuild();
            ISession session = sf.GetSession();


            System.Collections.IList messages = (System.Collections.IList)session.CreateCriteria(typeof(YTForever.Core.Domain.Message)).List();
            session.Close();

            return messages;

/////////////////////////////Cuyahoga-1.0 ///////////////////////////////改成自己的了,具体参考下载的源码http://www.cuyahoga-project.org/home/download.aspx.//////////////////////////////////

           private void RegisterCoreClasses()
        {       


            FileInfo  fInfo=new FileInfo(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
            if(!fInfo.Exists) return;

            /////////但dll文件好像不能读自己的config文件,需要自己加载.
      
            string sPath = fInfo.DirectoryName + @"\hibernate.cfg.xml";
             
           
            Configuration config = new Configuration().Configure(sPath);
            this._nhibernateConfiguration = config.AddAssembly(this.GetType().Assembly);
            this._nhibernateFactory = this._nhibernateConfiguration.BuildSessionFactory();
        }

//////////////////////////////不知道是加app.config那个文件可以不可以//////////////////////

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.0">
  <session-factory name="MyDoc.NHibernate.QuickStart">
    <!-- 属性 -->
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
    <property name="connection.connection_string">server=localhost;database=nhibernate;uid=sa;pwd=</property>
    <property name="show_sql">false</property>
    <property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>
    <property name="use_outer_join">true</property>
    <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
    <!-- 映射文件,这里有个assembly 好像 app.config的文件没有 -->
    <mapping assembly="YTForever.Core.Domain" />
  </session-factory>
</hibernate-configuration>