views:

1134

answers:

2

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.

A: 

I find suspicious those discriminator values might be keywords. Have you tried different values?

Rashack
I changed the values to ASQLProperty and AListProperty and it still errors out.
tom.dietrich
+1  A: 

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.

Jamie Ide
In my case ISqlLookupCompanyProperty and SqlLookupCompanyProperty are actually in different assemblies- Sample.Data and Sample.Core. All I want nHibernate to worry about is filling interfaces, does it really need to know every class that implements those interfaces?
tom.dietrich
AFAIK, it does need to know about every mapped class. What class do you expect it to return? It can't return an interface, it can only return a class declared as an interface.
Jamie Ide
I'd pass in classes from Sample.Core that implement the interface declared in Sample.Data. It's seeming like it's not possible to get a true three tier system with nHibernate, you've gotta declare part of your business logic objects in the data layer or nHibernate can't work with them.
tom.dietrich
If I understand you correctly, you want to pass a type (or an instance) that implements an interface and have NHibernate populate it for you based on a mapping to the interface. It doesn't work that way and I've never seen an O/R mapper or a DAL that works like that.I don't know if any of this has anything to do with your original question.
Jamie Ide
ahem... Your solution isn't the same as in the forum you link to. In the forum, there is a different discriminator for the interface and the concrete class. If you don't have this, NH could get confused when loading the data. (which type should be instantiated?)
Stefan Steinegger
@Stefan -- You're right, the discriminator values should be different. I blame it on the maroon, red, and violet color scheme in the sample code. And, like you, I still don't understand what the questioner is trying to achieve.
Jamie Ide
Lol, but you could ironically write the accepted answer :-)
Stefan Steinegger