每当我使用PersitenceSpecification类来验证具有对值对象的引用的实体时,我就会得到这个奇怪的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{ public CatalogItemMapping() { Id(catalogItem => catalogItem.Id); Component (category => category.Manufacturer, m => m.Map(manufacturer => manufacturer.Name)); Map(catalogItem => catalogItem.Name); Map(Reveal.Property ("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 (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.Colrow.Generic.List的System.ThrowHelper.ThrowArgumentOutOfRangeException()处
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(Object propertyValue)d:\ Builds\FluentNH\src\FluentNHibernate\Testing\PersistenceSpecification.cs(105,0):at FluentNHibernate.Testing.PersistenceSpecification`1.VerifyTheMappings()C:\ Source\SupplyChain\Tests\Retailer.IntegrationTests\Mappings\CatalogItemMappingSpecifications.cs(14,0):在SupplyChain.Retailer.IntegrationTests.Mappings.When_verifying_the_class_mapping_of_a_catalog_item.Then_a_catalog_object_should_be_persistable()
对不起,很长的帖子,但这个让我忙了几个小时了.这可能不是由FNH引起的,因为我发现NH本身的这张JIRA票据提到了类似的东西:
http://forum.hibernate.org/viewtopic.php?p=2395409
我仍然希望我的代码中出错了:-).任何想法?
提前致谢
我找到了解决这个问题的方法,这个问题首先源于我自己的愚蠢.一旦我从流畅的NH映射生成hbm文件,这一切都变得清晰.
... ...
请注意,Name属性的列和Manufacturer组件的列都映射到同一列.这就是为什么这导致了ArgumentOutOfRangeException,因为有更多的参数而不是列名.我通过明确指定组件映射的列名来解决这个问题:
组件(catalogItem => catalogItem.Manufacturer,m => m.Map(manufacturer => manufacturer.Name,"Manufacturer"));
另一个经验教训.