tom.dietrich
tom.dietrich

Reputation: 8337

Error building SessionFactory in nHibernate

I am getting an "ArgumentOutOfRangeException" on parameter index whenever I try to instantiate a session factory in nHibernate. The error comes from a procedure deep in the bowels of nHibernate. I can post the stack-trace of the error if that would help.

This is on a fresh project with a pretty simple three tier architecture. The data layer maps the nHibernate classes to simple interfaces which are implemented by the business layer. Currently there is only one interface set and mapping file being used by nHibernate. Hopefully my issue is easy to spot.

Here is the function where I build the sessionFactory.

  Private Shared Function SessionFactory() As ISessionFactory
        If _sessionFactory Is Nothing Then
            Dim config As New NHibernate.Cfg.Configuration
            config.Configure()
            config.AddClass(GetType(ICompanyProperty))
            _sessionFactory = config.BuildSessionFactory            
        End If
        Return _sessionFactory
    End Function

Here is my hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    <property name="connection.provider">
      NHibernate.Connection.DriverConnectionProvider     
    </property>
    <property name="dialect">
      NHibernate.Dialect.MsSql2005Dialect
    </property>
    <property name="connection.driver_class">
      NHibernate.Driver.SqlClientDriver
    </property>
    <property name="connection.connection_string">
      Data Source=dataserver.domain.ad;Initial Catalog=LABs;Integrated Security=SSPI
    </property>
  </session-factory>
</hibernate-configuration>

And the mapping file of the interface I am mapping.

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Sample.Data" namespace="Sample.Data">
  <class name="ICompanyProperty" table="CompanyProperty">
    <id name="CompanyPropertyID" column="CompanyPropertyID">
      <generator class="native" />
    </id>    
    <discriminator column="PropertyType" type="String" />
    <property name="CompanyID" />
    <property name="PropertyName" />    
    <subclass discriminator-value="SQL" name="ISQLLookupCompanyProperty">
      <property name="TableName" />      
      <property name="KeyField" />
      <property name="ValueField" />
      <property name="ConnectionString" />
    </subclass>
    <subclass discriminator-value="LIST" name="ILookupCompanyProperty" />    
  </class>
</hibernate-mapping>

And the interfaces

Public Interface ICompanyProperty
    Property CompanyPropertyID() As Integer
    Property PropertyName() As String
    Property CompanyID() As Integer
    Property PropertyType() As String
End Interface

Public Interface ILookupCompanyProperty
    Inherits ICompanyProperty
End Interface

Public Interface ISQLLookupCompanyProperty
    Inherits ILookupCompanyProperty

    Property TableName() As String
    Property KeyField() As String
    Property ValueField() As String
    Property ConnectionString() As String
End Interface

Thank you!

Edit: The stacktrace was requested. here it is from my call of build session factory to the error.

at System.Collections.ArrayList.get_Item(Int32 index)
at NHibernate.SqlCommand.InFragment.ToFragmentString()
at NHibernate.Persister.Entity.SingleTableEntityPersister.DiscriminatorFilterFragment(String alias)
at NHibernate.Persister.Entity.SingleTableEntityPersister.FilterFragment(String alias)
at NHibernate.Persister.Entity.AbstractEntityPersister.FilterFragment(String alias, IDictionary`2 enabledFilters)
at NHibernate.Loader.Entity.EntityJoinWalker..ctor(IOuterJoinLoadable persister, String[] uniqueKey, Int32 batchSize, LockMode lockMode, ISessionFactoryImplementor factory, IDictionary`2 enabledFilters)
at NHibernate.Loader.Entity.EntityLoader..ctor(IOuterJoinLoadable persister, String[] uniqueKey, IType uniqueKeyType, Int32 batchSize, LockMode lockMode, ISessionFactoryImplementor factory, IDictionary`2 enabledFilters)
at NHibernate.Loader.Entity.EntityLoader..ctor(IOuterJoinLoadable persister, Int32 batchSize, LockMode lockMode, ISessionFactoryImplementor factory, IDictionary`2 enabledFilters)
at NHibernate.Loader.Entity.EntityLoader..ctor(IOuterJoinLoadable persister, LockMode lockMode, ISessionFactoryImplementor factory, IDictionary`2 enabledFilters)
at NHibernate.Loader.Entity.BatchingEntityLoader.CreateBatchingEntityLoader(IOuterJoinLoadable persister, Int32 maxBatchSize, LockMode lockMode, ISessionFactoryImplementor factory, IDictionary`2 enabledFilters)
at NHibernate.Persister.Entity.AbstractEntityPersister.CreateEntityLoader(LockMode lockMode, IDictionary`2 enabledFilters)
at NHibernate.Persister.Entity.AbstractEntityPersister.CreateEntityLoader(LockMode lockMode)
at NHibernate.Persister.Entity.AbstractEntityPersister.CreateLoaders()
at NHibernate.Persister.Entity.AbstractEntityPersister.PostInstantiate()
at NHibernate.Persister.Entity.SingleTableEntityPersister.PostInstantiate()
at NHibernate.Impl.SessionFactoryImpl..ctor(Configuration cfg, IMapping mapping, Settings settings, EventListeners listeners)
at NHibernate.Cfg.Configuration.BuildSessionFactory()

Edit2: When I remove the discriminator I no longer get this error. Am I not doing that right?

Edit 3: When I download the source code for nHibernate, build it on my own, link to it's debug output and run it, I get a completely different error about ProxyFactoryFactory not being configured.

Upvotes: 0

Views: 3483

Answers (1)

Jamie Ide
Jamie Ide

Reputation: 49251

I think you need to tell NHibernate what concrete classes to use. To do that using interfaces, you need to nest the subclass mapping:

<subclass discriminator-value="SQL" name="ISQLLookupCompanyProperty">
  <subclass discriminator-value="SQL" name="SQLLookupCompanyProperty>
      <property name="TableName" />      
      <property name="KeyField" />
      <property name="ValueField" />
      <property name="ConnectionString" />
  </subclass>
</subclass>
<subclass discriminator-value="LIST" name="ILookupCompanyProperty">
    <subclass discriminator-value="LIST" name="LookupCompanyProperty" />
</subclass>

I found this solution in a blog entry.

Upvotes: 1

Related Questions