在nHibernate中构建SessionFactory时出错

时间:2009-04-28 17:50:52

标签: vb.net nhibernate

每当我尝试在nHibernate中实例化会话工厂时,我就会在参数索引上得到“ArgumentOutOfRangeException”。错误来自于nHibernate内部深处的一个过程。如果有帮助,我可以发布错误的堆栈跟踪。

这是一个带有非常简单的三层架构的新项目。数据层将nHibernate类映射到由业务层实现的简单接口。目前,nHibernate只使用一个接口集和映射文件。希望我的问题很容易被发现。

这是我构建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

这是我的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>

我正在映射的接口的映射文件。

<?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>

接口

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

谢谢!

编辑:请求了堆栈跟踪。这是从我对构建会话工厂的调用到错误。

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:当我删除鉴别器时,我不再收到此错误。我不是那样做的吗?

编辑3:当我下载nHibernate的源代码时,自己构建它,链接到它的调试输出并运行它,我得到一个完全不同的关于没有配置ProxyFactoryFactory的错误。

1 个答案:

答案 0 :(得分:1)

我认为你需要告诉NHibernate使用哪些具体的类。要使用接口执行此操作,您需要嵌套子类映射:

<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>

我在blog entry中找到了此解决方案。