作为一种通用的持久层框架,NH可以通过配置来指定所使用的数据库类型,并且可根据不同的数据库调整NH的运行参数。这些配置信息通常保存在配置文件中,当数据库配置发生改变时,我们只需要修改配置文件即可,不需要修改代码和编译。
主要内容:
1、NH支持的配置参数
2、设置NH配置参数的若干方法
一、NH支持的配置参数
NH初始化NHibernate.Cfg.Configuration对象时需要一系列的配置参数,最基本如数据库类型、访问方言和数据库连接字符串等。下表列出了常用的配置参数:
Attributes
|
Usage
|
Example (in default style)
|
hibernate.connection.provider_class
|
指定NH所使用的Nhibernate.Connection.IConnectionProvider类型。Required
|
<
nhibernate
>
<
add
key
="hibernate.connection.provider"
value
="NHibernate.Connection.DriverConnectionProvider"/>
</
nhibernate
>
|
hibernate.connection.isolation
|
指定数据库事务级别,同
System.Data.IsolationLevel
设定。Required
|
<
nhibernate
>
<
add
key
="hibernate.connection.isolation"
value
="ReadCommitted"/>
</
nhibernate
>
|
hibernate.connection.connection_string
|
指定数据库连接字符串。Required
|
<
nhibernate
>
<
add
key
="hibernate.connection.connection_string"
value
="Server=localhost;uid=sa;password=sa;database=NHTrial"
/>
</
nhibernate
>
|
hibernate.connection.driver_class
|
指定NH使用的NHibernate.Driver.IDriver类型。Required
|
<
nhibernate
>
<
add
key
="hibernate.connection.driver_class"
value
="NHibernate.Driver.SqlClientDriver"
/>
</
nhibernate
>
|
hibernate.connection.pool_size
|
指定NH的数据库连接池大小
|
<
add
key
="hibernate.connection.pool_size"
value
="2"
/>
|
hibernate.dialect
|
指定NH所使用的数据库方言
NHibernate.Dialect.Dialect
类型
|
<
add
key
="hibernate.dialect"
value
="NHibernate.Dialect.MsSql2000Dialect"
/>
|
hibernate.use_outer_join
|
指定是否允许用户在HQL中使用outer-join方式的查询
|
<
add
key
="hibernate.user_outer_join"
value
="true|false"
/>
|
hibernate.query.substitutions
|
指定HQL转换为SQL需要进行的特殊字符串(Token)替换,多个字符串之间用,隔开
|
<
add
key
="hibernate.query.substitions"
value
="hqlToken1=sqlToken1,hqlToken2=sqlToken2"
/>
|
hibernate.cache.provider_class
|
指定提供缓存服务的ICacheProvider。
|
<
add
key
="hibernate.cache.provider_class"
value
="NHibernate.Cache.HashtableCacheProvider"
/>
|
hibernate.cache.use_query_cache
|
指定查询结果是否缓存?
|
<
add
key
="hibernate.cache.use_query_cache"
value
="true|false"
/>
|
hibernate.default_schema
|
指定默认的数据库schema
|
<
add
key
="hibernate.default_schema"
value
="northwind.dbo"
/>
|
hibernate.prepare_sql
|
指定是否使用prepare的方式处理sql语句
|
<
add
key
="hibernate.prepare_sql"
value
="true|false"
/>
|
hibernate.session_factory_name
|
指定默认的ISessionFactory对象的名称(Name属性)
|
<
add
key
="hibernate.session_factory_name"
value
="foo_factory"
/>
|
对上表的说明:
1)对于所指定类型的数据库,IConnectionProvider负责提供相应的IDbConnection;IDriver提供了相应的数据库访问策略(strategy)。
2)Optional的属性都有各自的默认值,请直接查阅NHibernate Online Document。
二、设置NH配置参数的若干方式
NHibernate支持多种设置Configuration配置参数的方式,可以把配置参数写在web.config/app.config文件,也可以在创建Configuration对象时,把配置信息赋值给Configuration对象。
1、在web.config/app.config的NameValueSectionHandler类型的nhibernate自定义配置节中设置配置参数,NH将在创建Configuration对象时自动设置。
<xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section
name="nhibernate"
type="System.Configuration.NameValueSectionHandler"
/>
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;uid=sa;password=sa;database=NHTrial"
/>
</nhibernate>
<configuration>
然后,我们在程序中只需简单的初始化Configuration对象即可。
Configuration cfg = new Configuration();
2、使用nhibernate配置文件。

下面的nhibernate配置信息和我们刚才在web.config中的设置等价
<xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.0" >
<session-factory name="NHConsole">
<property name="connection.provider">NHibernate.Connection.DriverConnectionProviderproperty>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriverproperty>
<property name="connection.connection_string">Server=localhost;uid=sa;password=sa;database=NHTrialproperty>
<property name="dialect">NHibernate.Dialect.MsSql2000Dialectproperty>
session-factory>
</hibernate-configuration>
不过,我们要采取另一种方法来创建Configuration对象,语句中的fileName为nhibernate配置文件路径:
Configuration cfg = new Configuration().Configure(fileName);
等等,你说NH会不会有默认的nhibernate配置文件?当我们调用
Configuration cfg = new Configuration().Configure();
时,NH将自动查找web.config/app.config文件所在目录下是否存在名为hibernate.cfg.xml的配置文件,并以此文件中的配置信息来初始化Configuration对象。

3、在代码中设置配置参数。
最后,我们也可以在程序代码中通过给Configuration对象的配置属性赋值,如:
Configuration cfg = new Configuration();
cfg.Properties[NHibernate.Cfg.Environment.Dialect] = "NHibernate.Dialect.MsSql2000Dialect";
Programm._factory = cfg.BuildSessionFactory();
当然,我们一般不会像上面那段代码一样hardcode配置信息,但通过赋值的方式,我们可以自定义的方式去组织和读取配置信息(不同的团队往往有不同的配置信息组织的方式)。