针对大型系统,往往采用Oracle数据库。这是因为在大数据量的情况下Oracle的优势比较明显,而且能够部署到非Windows环境下,从而弥补.NET程序跨平台的缺点。但是使用NHibernate访问Oracle和访问SQL Server相比来说要略微复杂。
在使用NHibernate访问Oracle的过程中,我们需要以下几个步骤:
1.下载并安装Oracle客户端http://www.oracle.com/technology/software/products/database/oracle10g/htdocs/winsoft.html
2.确保驱动程序集文件(System.Data.OracleClient.dll或Oracle.DataAccess.dll)存在。
3.配置Spring.NET的Spring.Data.NHibernate.LocalSessionFactoryObject对象。
按照上面的步骤即可访问Oracle数据库。在Oracle10G和11G的环境下通过测试。 以下是Spring.Data.NHibernate.LocalSessionFactoryObject对象的配置方式。
Spring.Data.NHibernate.LocalSessionFactoryObject有两种配置方式:一种是配置HibernateProperties属性的每个entry节点,另一种是配置ConfigFilenames属性来从外部导入hibernate.cfg.xml文件。
首先,配置HibernateProperties属性的代码为:
NHibernateSessionFactory
<
object
id
="NHibernateSessionFactory"
type
="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate21"
>
<
property
name
="DbProvider"
ref
="DbProvider"
/>
<
property
name
="MappingAssemblies"
>
<
list
>
<
value
>
Domain
</
value
>
</
list
>
</
property
>
<
property
name
="HibernateProperties"
>
<
dictionary
>
<
entry
key
="hibernate.connection.provider"
value
="NHibernate.Connection.DriverConnectionProvider"
/>
<
entry
key
="dialect"
value
="NHibernate.Dialect.Oracle10gDialect"
/>
<
entry
key
="connection.driver_class"
value
="NHibernate.Driver.OracleClientDriver"
/>
<
entry
key
="use_outer_join"
value
="true"
/>
<
entry
key
="show_sql"
value
="true"
/>
<
entry
key
="hbm2ddl.auto"
value
="update"
/>
<
entry
key
="hibernate.current_session_context_class"
value
="Spring.Data.NHibernate.SpringSessionContext, Spring.Data.NHibernate21"
/>
<
entry
key
="proxyfactory.factory_class"
value
="NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu"
/>
</
dictionary
>
</
property
>
<
property
name
="ExposeTransactionAwareSessionFactory"
value
="true"
/>
</
object
>
注意的是:connection.driver_class属性配置为NHibernate.Driver.OracleClientDriver;dialect属性配置为NHibernate.Dialect.Oracle10gDialect。
数据库的provider配置为:
<
db:provider
id
="DbProvider"
provider
="System.Data.OracleClient"
connectionString
="User ID=system;Password=ld;Data Source=192.168.1.122/ORCL"
/>
也可以设置provider属性为:OracleClient-2.0。这样配置是以微软提供的System.Data.OracleClient作为驱动。如果要以甲骨文提供的Oracle.DataAccess.dll作为驱动,则在确保引用Oracle.DataAccess.dll程序集的情况下需用修改provider属性为:OracleODP-2.0,修改connection.driver_class属性为NHibernate.Driver.OracleDataClientDriver。
其次,配置ConfigFilenames属性外部导入hibernate.cfg.xml文件的代码如下:
NHibernateSessionFactory
<
object
id
="NHibernateSessionFactory"
type
="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate21"
>
<
property
name
="DbProvider"
ref
="DbProvider"
/>
<
property
name
="ConfigFilenames"
>
<
list
>
<
value
>
D:\Config\hibernate.cfg.xml
</
value
>
</
list
>
</
property
>
<
property
name
="ExposeTransactionAwareSessionFactory"
value
="true"
/>
</
object
>
hibernate.cfg.xml的配置为:
hibernate-configuration
<
hibernate-configuration
xmlns
="urn:nhibernate-configuration-2.2"
>
<
session-factory
name
="WebApp"
>
<!--
<property name="connection.connection_string">
User ID=system;Password=ld;Data Source=192.168.1.122/ORCL
</property>
-->
<
property
name
="show_sql"
>
false
</
property
>
<
property
name
="dialect"
>
NHibernate.Dialect.Oracle10gDialect
</
property
>
<
property
name
="connection.driver_class"
>
NHibernate.Driver.OracleClientDriver
</
property
>
<
property
name
="query.substitutions"
>
true 1, false 0, yes 'Y', no 'N'
</
property
>
<
property
name
="hbm2ddl.auto"
>
update
</
property
>
<
property
name
="proxyfactory.factory_class"
>
NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu
</
property
>
<
mapping
assembly
="Domain"
/>
</
session-factory
>
</
hibernate-configuration
>
好了,以上就是我的两种配置方式。
代码下载