1

私は基本クラス "エンティティ"を継承し、私のWCF RESTアプリケーションでFluent NHibernateでマッピングされた "Entity"というサブクラスと "Project"、 "Company"、 "Contact"というサブクラスを持っています。Fluent NHibernate Polymorphism and TransformUsing

以前は、エンティティクラスでこの多型関連は必要ありませんでしたが、最近このクラスを多対多の関係に関連付ける必要がありましたので、これを行うことにしました。

 public crmEntityMap() 
    { 
     Table("crmEntity"); 
     LazyLoad(); 
     Id(x => x.ID).GeneratedBy.Identity().Column("ID"); 
     Map(x => x.instanceID).Not.Nullable().Column("InstanceID"); 
     Map(x => x.comment).Column("Comment").Length(500); 
     HasManyToMany(x => x.RelatedEntities) 
      .AsList(i => i.Column("`Index`")) 
      .ParentKeyColumn("ParentID") 
      .ChildKeyColumn("ChildID") 
      .BatchSize(100) 
      .Not 
      .LazyLoad() 
      .Fetch.Join() 
      .Cascade.None(); 
     DiscriminateSubClassesOnColumn("Type"); 
    } 

プロジェクトマッピング:私のWCF RESTサービスのシリアル化

public class crmProjectMap : SubclassMap<crmProject> 
{ 

    public crmProjectMap() { 
     Table("crmProjects"); 
     LazyLoad(); 
     Map(x => x.name).Column("Name").Length(50); 
     Map(x => x.initialDate).Column("InitialDate"); 
     Map(x => x.deadline).Column("Deadline"); 
     Map(x => x.isClosed).Column("IsClosed"); 
     References(x => x.assignedToUser).Column("AssignedToUserID").NotFound.Ignore(); 
    } 
} 

変換コード:これが私の基本クラスのマッピングです

  public static DTO.Project GetProject(int projectId, int instanceId) 
     { 
      crmUser user = null; 
      return Provider.GetSession().QueryOver<crmProject>() 
       .Fetch(x => x.assignedToUser).Eager() 
       .JoinAlias(x => x.assignedToUser,() => user, JoinType.LeftOuterJoin) 
       .Where(c => c.ID == projectId) 
       .And(c => c.instanceID == instanceId) 
    .Select(Projections.ProjectionList() 
     .Add(Projections.Property("ID"), "ID") 
     .Add(Projections.Property("instanceID"), "instanceID") 
     .Add(Projections.Property("name"), "name") 
     .Add(Projections.Property("comment"), "comment") 
     .Add(Projections.Property("isClosed"), "isClosed") 
     .Add(Projections.Property("initialDate"), "initialDate") 
     .Add(Projections.Property("deadline"), "deadline") 
     .Add(Projections.Property(() => user.userID), "assignedToUserID") 
     //Add Related Entities? 
).TransformUsing(Transformers.AliasToBean<DTO.Project>()) 
    .SingleOrDefault<DTO.Project>(); 
     } 

しかし、あなたが見ることができるように私は必要ここにRelatedEntitiesを追加する方法はわかりませんが、RelatedEntityはEntityクラスを継承する "Company"、 "Contact"または "Project"になる可能性があるため、これを行う方法はわかりません。私はこれをDTO.Projectクラスで定義し、それにデータを変換する必要があります。

答えて

0

これは私のこの問題に関する修正です。誰かがそれを改善できれば幸いです。

 DTO.Project proj = new DTO.Project(); 
     crmUser user = null; 
     crmEntity ent =null; 
     using (ISession session = Provider.SessionFactory.OpenSession()) 
     { 
      proj = session.QueryOver<crmProject>() 
       .Fetch(x => x.assignedToUser).Eager() 
       .JoinAlias(x => x.assignedToUser,() => user, JoinType.InnerJoin) 
       .Where(c => c.ID == projectId) 
       .And(c => c.instanceID == instanceId) 
    .Select(Projections.ProjectionList() 
     .Add(Projections.Property("ID"), "ID") 
     .Add(Projections.Property("instanceID"), "instanceID") 
     .Add(Projections.Property("name"), "name") 
     .Add(Projections.Property("comment"), "comment") 
     .Add(Projections.Property("isClosed"), "isClosed") 
     .Add(Projections.Property("initialDate"), "initialDate") 
     .Add(Projections.Property("deadline"), "deadline") 
     .Add(Projections.Property(() => user.userID), "assignedToUserID") 
).TransformUsing(Transformers.AliasToBean<DTO.Project>()) 
    .SingleOrDefault<DTO.Project>(); 

      proj.RelatedEntities = session.QueryOver<crmProject>() 
       .Fetch(x => x.RelatedEntities).Eager() 
       .Where(c => c.ID == projectId).JoinAlias(x=>x.RelatedEntities,()=>ent,JoinType.InnerJoin) 
    .Select(Projections.ProjectionList() 
     .Add(Projections.Property(()=>ent.ID), "ID") 
     .Add(Projections.Property(() => ent.type), "Type") 
).TransformUsing(Transformers.AliasToBean<DTO.EntityRelation>()).List<DTO.EntityRelation>(); 
     } 
     return proj;