Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate 3.0 configuration with ODP.NET

Tags:

nhibernate

I just setup NHibernate for the first time. My platform and config settings as follows:

  • Database: Oracle 11.1g
  • ODP.NET Version: 4.112.1.2 (Installed from ODTWithODAC112012, which is a 1 release above my Oracle db installation)
  • NHibernate Version 3.0

I created a test MVC app with a test project. Then, to test for NHibernate connection, I use the following test fixture:

using IBCService.Models;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using NUnit.Framework;
namespace IBCService.Tests
{
    [TestFixture]
    public class GenerateSchema_Fixture
    {
        [Test]
        public void Can_generate_schema()
        {
            var cfg = new Configuration();
            cfg.Configure();
            cfg.AddAssembly(typeof(Product).Assembly);
            new SchemaExport(cfg).Execute(false, true, false);
        }
    }

The Nhibernate config file:

<?xml version="1.0" encoding="utf-8"?>
<!-- This config use Oracle Data Provider (ODP.NET) -->
<hibernate-configuration  xmlns="urn:nhibernate-configuration-2.2" >
  <session-factory name="NHibernate.Test">
    <property name="connection.driver_class">NHibernate.Driver.OracleDataClientDriver</property>
    <property name="connection.connection_string">
      User ID=TEST;Password=******;Data Source=//RAND
    </property>
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="show_sql">false</property>
    <property name="dialect">NHibernate.Dialect.Oracle10gDialect</property>
    <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
    <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
  </session-factory>
</hibernate-configuration>

On test, I get the following exception stack trace:

NHibernate.HibernateException was unhandled by user code
  Message=Could not create the driver from NHibernate.Driver.OracleDataClientDriver.
  Source=NHibernate
  StackTrace:
       at NHibernate.Connection.ConnectionProvider.ConfigureDriver(IDictionary`2 settings) in d:\CSharp\NH\nhibernate\src\NHibernate\Connection\ConnectionProvider.cs:line 113
       at NHibernate.Connection.ConnectionProvider.Configure(IDictionary`2 settings) in d:\CSharp\NH\nhibernate\src\NHibernate\Connection\ConnectionProvider.cs:line 64
       at NHibernate.Connection.ConnectionProviderFactory.NewConnectionProvider(IDictionary`2 settings) in d:\CSharp\NH\nhibernate\src\NHibernate\Connection\ConnectionProviderFactory.cs:line 50
       at NHibernate.Tool.hbm2ddl.SchemaExport.Execute(Action`1 scriptAction, Boolean export, Boolean justDrop) in d:\CSharp\NH\nhibernate\src\NHibernate\Tool\hbm2ddl\SchemaExport.cs:line 333
       at NHibernate.Tool.hbm2ddl.SchemaExport.Execute(Boolean script, Boolean export, Boolean justDrop) in d:\CSharp\NH\nhibernate\src\NHibernate\Tool\hbm2ddl\SchemaExport.cs:line 290
       at IBCService.Tests.GenerateSchema_Fixture.Can_generate_schema() in D:\APPS\VS2010\IBanking\CustomerService\IBCService.Tests\GenerateSchema_Fixture.cs:line 21
  InnerException: System.Reflection.TargetInvocationException
       Message=Exception has been thrown by the target of an invocation.
       Source=mscorlib
       StackTrace:
            at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
            at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache)
            at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache)
            at System.Activator.CreateInstance(Type type, Boolean nonPublic)
            at System.Activator.CreateInstance(Type type)
            at NHibernate.Bytecode.ActivatorObjectsFactory.CreateInstance(Type type) in d:\CSharp\NH\nhibernate\src\NHibernate\Bytecode\ActivatorObjectsFactory.cs:line 9
            at NHibernate.Connection.ConnectionProvider.ConfigureDriver(IDictionary`2 settings) in d:\CSharp\NH\nhibernate\src\NHibernate\Connection\ConnectionProvider.cs:line 107
       InnerException: System.NullReferenceException
            Message=Object reference not set to an instance of an object.
            Source=NHibernate
            StackTrace:
                 at NHibernate.Driver.OracleDataClientDriver..ctor() in d:\CSharp\NH\nhibernate\src\NHibernate\Driver\OracleDataClientDriver.cs:line 42
            InnerException: 

If I change NHibernate.Driver.OracleDataClientDriver to NHibernate.Driver.OracleClientDriver (MS provider for Oracle), the test succeed. Can someone tell me what I'm doing wrong?

like image 225
Olu Lawrence Avatar asked Dec 30 '10 10:12

Olu Lawrence


2 Answers

Hi I think the error happens, because Nhibernate is not loading the driver from the GAC with Assembly.LoadWithPartialName(), but with Assembly.Load(). Try to place Oracle.DataAccess.dll in the bin directory or use qualifyAssembly section into your app.config or web.config. Example:

<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<qualifyAssembly partialName="Oracle.DataAccess"
fullName="Oracle.DataAccess, Version=2.102.2.20, Culture=neutral, PublicKeyToken=89b483f429c47342" />
</assemblyBinding>
</runtime>
like image 150
Dani Avatar answered Nov 17 '22 04:11

Dani


@PerlDev: I think that you have a "ODP.NET 32 bit binary" installed and you are compiling your app with platform "AnyCPU". If so, try to change to x86 (this is what I did, and worked).

If you want to compile as x64 I think you must install the "ODP.NET 64 bit binary" (I didn´t do it yet).

like image 39
Andrés Falcón Avatar answered Nov 17 '22 06:11

Andrés Falcón