代码之家  ›  专栏  ›  技术社区  ›  anakic

代码中的nhibernate子类

  •  0
  • anakic  · 技术社区  · 15 年前

    我想通过代码在nhibernate中设置每个类层次结构的表继承。除子类外,其他所有内容都在XML映射文件中设置。如果我在xml中创建子类,一切都很好,但不是从代码中创建的。这是我使用的代码-我的具体子类从未创建过:(

    //the call
    NHibernate.Cfg.Configuration config = new NHibernate.Cfg.Configuration();
    SetSubclass(config, typeof(TAction), typeof(tActionSub1), "Procedure");
    
    //the method
    public static void SetSubclass(Configuration configuration, Type baseClass, Type subClass, string discriminatorValue)
    {
                PersistentClass persBaseClass = configuration.ClassMappings.Where(cm => cm.MappedClass == baseClass).Single();
                SingleTableSubclass persSubClass = new SingleTableSubclass(persBaseClass);
                persSubClass.ClassName = subClass.AssemblyQualifiedName;
                persSubClass.DiscriminatorValue = discriminatorValue;
                persSubClass.EntityPersisterClass = typeof(SingleTableEntityPersister);
                persSubClass.ProxyInterfaceName = (subClass).AssemblyQualifiedName;
                persSubClass.NodeName = subClass.Name;
                persSubClass.EntityName = subClass.FullName;
                persBaseClass.AddSubclass(persSubClass);
    }
    

    Xml映射如下所示:

    <?xml version="1.0" encoding="utf-8" ?>
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Riz.Pcm.Domain.BusinessObjects" assembly="Riz.Pcm.Domain">
      <class name="Riz.Pcm.Domain.BusinessObjects.TAction, Riz.Pcm.Domain" table="dbo.tAction" lazy="true">
        <id name="Id" column="ID">
          <generator class="guid" />
        </id>
        <discriminator type="String" formula="(select jt.Name from TJobType jt where jt.Id=JobTypeId)" insert="true" force="false"/>
        <many-to-one name="Session" column="SessionID" class="TSession" />
        <property name="Order" column="Order1" />
        <property name="ProcessStart" column="ProcessStart" />
        <property name="ProcessEnd" column="ProcessEnd" />
        <property name="Status" column="Status" />
        <many-to-one name="JobType" column="JobTypeID" class="TJobType" />
        <many-to-one name="Unit" column="UnitID" class="TUnit" />
        <bag name="TActionProperties" lazy="true" cascade="all-delete-orphan" inverse="true" >
          <key column="ActionID"></key>
          <one-to-many class="TActionProperty"></one-to-many>
        </bag>
        <!--<subclass name="Riz.Pcm.Domain.tActionSub" discriminator-value="ZPower"></subclass>-->
      </class>
    </hibernate-mapping>
    

    我做错什么了?我在谷歌上找不到任何例子:(

    1 回复  |  直到 15 年前
        1
  •  0
  •   isuruceanu    15 年前

    我们正在使用FluentNHibernate进行映射。用户实体是学习者、评估者、管理员等的基类。

    举个例子:

     public class UserMap : ClassMap<User>
    {
        public UserMap()
        {
            this.Id(x => x.Id);
    
            this.Map(x => x.Active);
    
            this.Component(
                x => x.Address,
                m =>
                {
                    m.Map(x => x.Address1).Length(512);
                    m.Map(x => x.Address2);
                    m.Map(x => x.Address3);
                    m.Map(x => x.Address4);
                    m.Map(x => x.City);
                    m.Map(x => x.County);
                    m.Map(x => x.PostCode);
                    m.References(x => x.Country);
                });
    
            this.References(x => x.CreatedBy);
    
            this.Map(x => x.CreatedDate).Not.Nullable();
    
            this.DiscriminateSubClassesOnColumn("className").Length(64);
        }
    }
    

    和派生类

    public class LearnerMap : SubclassMap<Learner>
    {
        #region Constructors and Destructors
    
        /// <summary>
        /// Initializes a new instance of the <see cref="LearnerMap"/> class.
        /// </summary>
        public LearnerMap()
        {
            this.Map(x => x.DateOfBirth);
            this.Map(x => x.NINumber);
            this.Map(x => x.IscCodeId);
            this.Map(x => x.ScnNo);
            this.Map(x => x.ULN);
    
    
            this.HasManyToMany(x => x.Organisation)
                .ParentKeyColumn("userId")
                .ChildKeyColumn("organisationId").Table(
                "UserOrganisations");
        }
    

    希望这会有帮助