1

我有一个使用映射配置文件的 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 多少次)。这可能是因为我将某些实体的密钥生成从 转换IdentityHiLo.

所以在我看来,我的代码正在创建一个导致问题的特定配置,希望这不会再次困扰我。

如果我猜到了,我会说导致问题的配置涉及对长时间运行的事务的轻率使用与身份密钥生成的结合。

4

1 回答 1

2

我在很多项目中使用了混合映射,并且没有遇到您描述的任何问题。我不明白为什么冲洗需要 12 秒。

我的混合映射技术与您的略有不同,我不能 100% 确定order您的配置是否重要,值得一试。见http://puredotnetcoder.blogspot.co.uk/2011/07/mixed-mappings-with-hbm-and-mapping-by.html

我认为您已导出所有映射并仔细检查它们前后是否相同。见http://puredotnetcoder.blogspot.co.uk/2011/07/view-xml-generated-when-mapping-by-code.html

于 2012-12-12T09:00:15.680 回答