每当我使用
PersitenceSpecification类,用于验证具有以下内容的实体
引用值对象。
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)
{}
}
这是我得到的错误:
抱歉,很长的帖子,但是这个让我很忙
几个小时了。这可能不是由FNH引起的,因为我发现了这张JIRA机票
NH本身提到类似的内容:
http://forum.hibernate.org/viewtopic.php?p=2395409
我仍然希望我在代码中做错了:-)。任何
想法?
提前致谢
最佳答案
我找到了解决这个问题的方法,这是由我自己造成的
首先是愚蠢。我一想到这一切就明白了
通过流畅的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>
请注意,“名称”属性的列和“属性”的列
制造商组件都映射到同一列。这就是为什么
这导致了ArgumentOutOfRangeException,因为
参数多于列名。我解决了
明确指定组件映射的列名:
Component(catalogItem => catalogItem.Manufacturer,
m => m.Map(manufacturer => Manufacturer.Name,
“制造商”));
另一个教训。