2009-03-01 120 views
5

無論何時我使用 PersitenceSpecification類驗證對值對象具有 引用的實體,我都會收到此奇怪的ArgumentOutOfRangeException。ArgumentOutOfRangeException:索引超出範圍

public class CatalogItem : DomainEntity 
    { 
     internal virtual Manufacturer Manufacturer { get; private 
set; } 
     internal virtual String Name { get; private set; } 

     protected CatalogItem() 
     {} 

     public CatalogItem(String name, String manufacturer) 
     { 
      Name = name; 
      Manufacturer = new Manufacturer(manufacturer); 
     } 
    } 

    public class CatalogItemMapping : ClassMap<CatalogItem> 
    { 
     public CatalogItemMapping() 
     { 
      Id(catalogItem => catalogItem.Id); 

      Component<Manufacturer>(category => category.Manufacturer, 
            m => m.Map(manufacturer => 
manufacturer.Name)); 

      Map(catalogItem => catalogItem.Name); 
      Map(Reveal.Property<CatalogItem>("Price")); 
     } 
    } 

    [TestFixture] 
    public class When_verifying_the_class_mapping_of_a_catalog_item 
     : NHibernateSpecification 
    { 
     [Test] 
     public void Then_a_catalog_object_should_be_persistable() 
     { 
      new PersistenceSpecification<CatalogItem>(Session) 
       .VerifyTheMappings(); 
     } 
    } 

    [TestFixture] 
    public class NHibernateSpecification 
     : Specification 
    { 
     protected ISession Session { get; private set; } 

     protected override void Establish_context() 
     { 
      var configuration = new SQLiteConfiguration() 
       .InMemory() 
       .ShowSql() 
       .ToProperties(); 

      var sessionSource = new SessionSource(configuration, new 
RetailerPersistenceModel()); 
      Session = sessionSource.CreateSession(); 

      sessionSource.BuildSchema(Session); 
      ProvideInitialData(Session); 

      Session.Flush(); 
      Session.Clear(); 
     } 

     protected override void Dispose_context() 
     { 
      Session.Dispose(); 
      Session = null; 
     } 

     protected virtual void ProvideInitialData(ISession session) 
     {} 
    } 

這裏是我得到的錯誤:

的TestCase 'Then_a_catalog_object_should_be_persistable' 不執行: System.ArgumentOutOfRangeException: 指數超出範圍。必須爲 非負值且小於 的大小。參數名:指數 在System.ThrowHelper.ThrowArgumentOutOfRangeException (ExceptionArgument說法, ExceptionResource資源) 在System.ThrowHelper.ThrowArgumentOutOfRangeException() 在System.Collections.Generic.List 1.get_Item(Int32 index) at System.Data.SQLite.SQLiteParameterCollection.GetParameter(Int32 index) at System.Data.Common.DbParameterCollection.System.Collections.IList.get_Item (Int32 index) at NHibernate.Type.GuidType.Set(IDbCommand cmd, Object value, Int32 index) at NHibernate.Type.NullableType.NullSafeSet(IDbCommand cmd, Object value, Int32 index) at NHibernate.Type.NullableType.NullSafeSet(IDbCommand st, Object value, Int32 index, ISessionImplementor session) at NHibernate.Persister.Entity.AbstractEntityPersister.Dehydrate (Object id, Object[] fields, Object rowId, Boolean[] includeProperty, Boolean[][] includeColumns, Int32 table, IDbCommand statement, ISessionImplementor session, Int32 index) at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Boolean[] notNull, Int32 j, SqlCommandInfo sql, Object obj, ISessionImplementor session) at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Object obj, ISessionImplementor session) at NHibernate.Action.EntityInsertAction.Execute() at NHibernate.Engine.ActionQueue.Execute(IExecutable executable) at NHibernate.Engine.ActionQueue.ExecuteActions(IList list) at NHibernate.Engine.ActionQueue.ExecuteActions() at NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions (IEventSource session) at NHibernate.Event.Default.DefaultFlushEventListener.OnFlush (FlushEvent event) at NHibernate.Impl.SessionImpl.Flush() at NHibernate.Transaction.AdoTransaction.Commit() d:\Builds\FluentNH\src\FluentNHibernate\Testing \PersistenceSpecification.cs(127,0): at FluentNHibernate.Testing.PersistenceSpecification 1.TransactionalSave (對象的PropertyValue) d: \構建\ FluentNH \ SRC \ FluentNHibernate \測試 \ PersistenceSpecification.cs(105,0): 在 FluentNHibernate.Testing.PersistenceSpecification`1.VerifyTheMappings () C:\源\供應鏈\測試\ Retailer.IntegrationTests \映射 \ CatalogItemMappingSpecifications.cs(14,0): 在 SupplyChain.Retailer.IntegrationTests.Mappings.When_verifying_the_class_mapping_of_a_catalog_item.Then_a_catalog_object_should_be_persistable ()

很抱歉的長期職位,但是這一次讓我忙了幾個小時 現在。

http://forum.hibernate.org/viewtopic.php?p=2395409

我仍然希望我做錯了什麼在我的代碼:-):這可能不是FNH因爲我發現NH本身的這個JIRA票 即提到類似的情況引起。任何 想法?

在此先感謝

回答

15

我找到了解決這個問題的方法,這個問題首先是由我自己的 愚蠢造成的。 I 從流利的NH映射中生成了hbm文件,這一切都變得清晰起來。

<class name="CatalogItem" table="`CatalogItem`" xmlns="urn:nhibernate- 
mapping-2.2" optimistic-lock="version"> 
    ... 

    <property name="Name" length="100" type="String"> 
     <column name="Name" /> 
    </property> 

    ... 

    <component name="Manufacturer" insert="false" update="true"> 
     <property name="Name" length="100" type="String"> 
     <column name="Name" /> 
     </property> 
    </component> 
    </class> 

注意,Name屬性和用於 廠商組件列的列都被映射到相同的列中。這就是爲什麼 這導致了一個ArgumentOutOfRangeException,因爲有 更多的參數比列名稱。我解決了這個由 爲組件映射明確地指定列名:

組分(catalogItem => catalogItem.Manufacturer, 米=> m.Map(製造商=> manufacturer.Name, 「製造商」));

另一個教訓。

0

CatalogItem似乎並不有Price屬性,當你使用Reveal助手似乎很奇怪。

0

是的,我刪除了一個用於減少一些噪音。我想我忘了從映射中刪除它。在做了更多的調查之後,我注意到它與製造商被映射爲組件相關。當我使用一個普通的舊字符串而不是一個單獨的類時,一切正常。

0

在我的特殊情況下,我在相同的.NET屬性中添加了屬性以及ID(使用屬性)。這導致了相同的錯誤。

2

在我的情況下,我用Fluent NHibernate將兩個屬性映射到同一列。