2010-02-11 100 views
3

我与映射挣扎以下SQL表流利的NHibernate映射问题:多对多自加入额外的数据

|Post    |   |PostRelation  | 
    |------------------|   |-----------------| 
    |PostId   |1--------*|ParentPostId  | 
    |---other stuff--- |1--------*|ChildPostId  | 
    |     |   |RelationType  | 

理想编号喜欢叫relatedPosts上发表财产

Dictionary <RelationType,IList<Post>> 

但在最后一分钟,刚刚解决一个财产对邮政与

IList<PostRelation>. 

我s成功地使用了多对多来获得相关帖子,但是这种方法失去了附加数据。

有什么建议?

回答

4

我终于找到了一个解决方案,经过很多研究。所以尽管我会张贴它,以防将来帮助其他人。由于PostRelation有其他数据,因此它需要成为一个实体。

--- PostRelationMap

 Id(x => x.Id, "PostRelationId").GeneratedBy.Identity(); 

     References(x => x.ParentPost, "ParentPostId") 
      .ForeignKey("FK_PostRelation_ParentPost") 
      .Fetch.Join() 
      .LazyLoad(); 

     References(x => x.ChildPost, "ChildPostId") 
      .ForeignKey("FK_PostRelation_ChildPost") 
      .Fetch.Join() 
      .LazyLoad(); 

     Map(x => x.RelationshipType).CustomType<int>().Not.Nullable(); 

--- PostMap

HasMany(x => x.ChildPosts) 
      .Access.CamelCaseField(Prefix.Underscore) 
      .Cascade.AllDeleteOrphan() 
      .KeyColumn("ChildPostId") 
      .LazyLoad();