其實nh的官網提供的配置方式有不少種,我在這總結咱們最最經常使用的一些方法。html
http://www.cnblogs.com/gooddasenlin/archive/2008/08/21/1273581.html 參考這篇文章spring
多種配置方式混合使用
sql
(1)寫在config文件中,這樣會致使config文件太大,很差管理session
ToDo:配置文件的代碼例子app
<?xml version="1.0"?> <configuration> <configSections> <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" /> </configSections> <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.MsSql2008Dialect</property> <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property> <property name="connection.connection_string">Server=.;initial catalog=Db0515Demo;Integrated Security=SSPI</property> <mapping assembly="NHDemo" /> </session-factory> </hibernate-configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/> </startup> </configuration>
(2)引用外部文件的寫法ide
1)應用外部文件通常能夠從程序獲取外部文件的時候選擇路徑,但我這裏只說配置spa
2)他有一個默認的就是在config相同的目錄下面的找名稱爲hibernate.cfg.xml名稱的文件,還有要把這個文件的屬性設置爲老是複製。
.net
<?xml version="1.0" encoding="utf-8" ?> <!--SQL配置方式--> <!--<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> <session-factory> <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property> <property name="connection.connection_string">Server=(local);initial catalog=MyNHibernate;Integrated Security=SSPI</property> <property name="connection.isolation">ReadCommitted</property> <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property> </session-factory> </hibernate-configuration>--> <!--<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> <session-factory> <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property> <property name="connection.connection_string">Server=(local);initial catalog=MyNHibernate;Integrated Security=SSPI</property> <property name="connection.isolation">ReadCommitted</property> <property name="proxyfactory.factory_class"> NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle </property> </session-factory> </hibernate-configuration>--> <!--Oracle配置方式--> <!--<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> <session-factory> <property name="dialect">NHibernate.Dialect.Oracle10gDialect</property> <property name="connection.connection_string">User ID=apt;Password=pwd;Data Source=orcl</property> <property name="connection.isolation">ReadCommitted</property> <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property> </session-factory> </hibernate-configuration>--> <!--Oracle配置方式(最新版本Nhibernate3以上)這些能夠直接去官網複製--> <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" > <session-factory name="MyManager"> <property name="connection.driver_class">NHibernate.Driver.OracleClientDriver</property> <property name="dialect">NHibernate.Dialect.Oracle10gDialect</property> <property name="connection.connection_string">User ID=apt;Password=pwd;Data Source=orcl</property> <property name="show_sql">true</property> <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property> </session-factory> </hibernate-configuration> <!--<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> <session-factory> --> <!--<property name="connection.connection_string">Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=11.101.9.54)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=ORCL)));user id=C##MSWGR;password=mswgr;</property>--> <!-- --> <!-- <property name="default_schema">AdventureWorksLT.SalesLT</property>--> <!-- <property name="connection.connection_string">User ID=apt;Password=pwd;Data Source=orcl</property> <property name="dialect">NHibernate.Dialect.Oracle10gDialect</property> --> <!-- <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>--> <!-- <property name="connection.driver_class">NHibernate.Driver.OracleClientDriver</property> <property name="show_sql">true</property> <property name="connection.release_mode">auto</property> <property name="adonet.batch_size">500</property> <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property> --> <!-- hbm2ddl tool property should NOT be used in production and is here to get you going with the Cookbook! --> <!-- <property name="hbm2ddl.auto">update</property> <property name="proxyfactory.factory_class"> NHibernate.ByteCode.Castle.ProxyFactoryFactory,NHibernate.ByteCode.Castle</property> --> <!-- Mapping assemblies --> <!-- --> <!-- Can't map it for Fluent NHibernate here; instead, load the mapping assembly in Global.asax.cs. If you're still using HBMs, you can use the mapping here or pass the assembly via Global.asax.cs as well, just like you can do with the Fluent NHibernate assembly(s). --> <!-- </session-factory> </hibernate-configuration>-->
3)ToDo:各個參數內容的意思
hibernate
4)外部文件引用位置的一個問題code
個人配置文件是放在UI層項目中的,而後在test程序集中沒有引用,因此報的錯誤,解決辦法就是在test程序集中添加對UI層的引用,問題截圖。
(3)用fluent這種代碼配置的方式
(4)集成在spring.net這種IoC容器中的配置(主要是一些寫法的不一樣)
Loquacious配置