我有一个使用映射配置文件的NHibernate项目。我正在使用SQL Server数据库。
我想通过代码切换到映射。我的方法是一次完成一个类,确认每次转换都会通过所有测试。
混合两个映射是非常直接的:
public static ISessionFactory SessionFactory
{
get
{
if (_sessionFactory == null)
{
var configuration = new Configuration();
configuration.Configure();
configuration.AddAssembly(typeof(Entities.Player).Assembly);
var mapper = new NHibernate.Mapping.ByCode.ModelMapper();
// Here are the entities I've switched to mapping-by-code
DATMedia.CMS.EntityLibrary.Mappings.ScheduleMediaItem.Map(mapper);
DATMedia.CMS.EntityLibrary.Mappings.Schedule.Map(mapper);
configuration.AddMapping(mapper.CompileMappingForAllExplicitlyAddedEntities());
_sessionFactory = configuration.BuildSessionFactory();
}
return _sessionFactory;
}
}
但是,当我将Schedule
映射更改为按代码映射时,我遇到了主要的性能问题。拨打Session.Flush
需要12秒钟,而这需要花费大量的测试数据。
我切换回XML映射,性能问题消失了。
还有其他人遇到过这个问题吗?
我将包含schedule
的前后映射,以防万一有明显的缺陷:
通过配置文件:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="DATMedia.CMS.EntityLibrary.Entities.Schedule, DATMedia.CMS.EntityLibrary" table="cms_Schedule">
<id name="Id" column="Id" type="Int64">
<generator class="identity" />
</id>
<property name="Date" column="Date" type="Timestamp" />
<many-to-one name="SourceTemplate" column="SourceTemplate_Id" class="DATMedia.CMS.EntityLibrary.Entities.ScheduleTemplate, DATMedia.CMS.EntityLibrary" cascade="none" fetch="join" lazy="proxy"/>
<!--
Note that we are not using cascading deletes here.
This will be handled by SQL Server through ON DELETE CASCADE foreign key constraints
-->
<bag name="MediaItems" inverse="true" cascade="save-update" lazy="true" order-by="PlayIndex">
<key column="Schedule_Id" />
<one-to-many class="DATMedia.CMS.EntityLibrary.Entities.ScheduleMediaItem, DATMedia.CMS.EntityLibrary"/>
</bag>
<bag name="PlayerGroups" table="cms_ManyToMany_PlayerGroupSchedules_PlayerGroup_Schedule" lazy="true" cascade="save-update">
<key column="Schedule_Id" />
<many-to-many column="PlayerGroup_Id"
class="DATMedia.CMS.EntityLibrary.Entities.PlayerGroup, NHibernateManyToMany" />
</bag>
</class>
</hibernate-mapping>
按代码映射:
public static void Map(ModelMapper mapper)
{
mapper.Class<DATMedia.CMS.EntityLibrary.Entities.Schedule>(
classMapper =>
{
classMapper.Table("cms_Schedule");
classMapper.Id(x => x.Id, map =>
{
map.Column("Id");
map.Generator(Generators.Identity);
});
classMapper.Property(
s => s.Date,
propertyMapper =>
{
propertyMapper.Column("Date");
propertyMapper.NotNullable(true);
}
);
classMapper.ManyToOne(
s => s.SourceTemplate,
manyToOneMapper =>
{
manyToOneMapper.Column("SourceTemplate_Id");
manyToOneMapper.Cascade(Cascade.None);
manyToOneMapper.Fetch(FetchKind.Join);
manyToOneMapper.Lazy(LazyRelation.Proxy);
}
);
classMapper.Bag(
s => s.MediaItems,
bagPropertyMapper =>
{
bagPropertyMapper.Key(keyMapper =>
{
keyMapper.Column("Schedule_Id");
}
);
bagPropertyMapper.Inverse(true);
bagPropertyMapper.Cascade(Cascade.Persist);
bagPropertyMapper.Lazy(CollectionLazy.Lazy);
bagPropertyMapper.OrderBy(smi => smi.PlayIndex);
}
);
classMapper.Bag(
s => s.PlayerGroups,
bagPropertyMapper =>
{
bagPropertyMapper.Key(keyMapper =>
{
keyMapper.Column("Schedule_Id");
});
bagPropertyMapper.Table("cms_ManyToMany_PlayerGroupSchedules_PlayerGroup_Schedule");
bagPropertyMapper.Lazy(CollectionLazy.Extra);
bagPropertyMapper.Cascade(Cascade.Persist);
},
collectionElementRelation =>
{
collectionElementRelation.ManyToMany(manyToManyMapper =>
{
manyToManyMapper.Column("PlayerGroup_Id");
}
);
}
);
}
);
}
<小时/> 稍后修改
我通过不在交易中调用Flush
来解决问题。
我试图创建一些更简单的测试代码来复制问题,但是一直都没有成功(无论我多少次调用Flush,我的所有测试代码都运行得非常快)。这可能是因为我将某些实体的密钥生成从Identity
转换为HiLo
。
所以在我看来,我的代码创建了一个特殊的配置,导致了一个问题,希望这不会再困扰我了。
如果我猜到,我会说导致问题的配置涉及将长时间运行的事务与身份密钥生成结合起来的轻率使用。
答案 0 :(得分:2)
我在很多项目中使用了混合映射,并且没有遇到任何问题。我不明白为什么同花顺需要12秒。
我的混合映射技术与你的技术略有不同,我不能100%确定你配置的order
是否重要,值得一试。见http://puredotnetcoder.blogspot.co.uk/2011/07/mixed-mappings-with-hbm-and-mapping-by.html
我认为你已经导出了所有的映射,然后仔细检查它们之前和之后的 ARE 是否相同。见http://puredotnetcoder.blogspot.co.uk/2011/07/view-xml-generated-when-mapping-by-code.html