流畅的nhibernate m-to-m映射与外部表

时间:2015-04-02 13:03:26

标签: nhibernate fluent-nhibernate mapping fluent-nhibernate-mapping

我在Oracle中有两个表

Entity
----------
**EntityId** NUMBER(9), **EntityName** VARCHAR2

EntityLinks
--------------
**EntityLinkId** NUMBER(9),**ParentEntityId** NUMBER(9), **ChildEntityId** NUMBER(9)

表EntityLinks将存储各种实体之间的ManyToMany关系。 ParentEntityId ChildEntityId 实体具有外键关系。

我下面的实体以下是

public class Entity
{
     public virtual int EntityId {get; set}
     public virtual IList<Entity> ParentEntities {get; set}
     public virtual IList<Entity> ChildEntities{get; set}
}

public class EntityLinks
{
     public virtual int EntityLinkId {get; set}
     public virtual Entity ParentEntityId {get; set}
     public virtual Entity ChildEntityId {get; set}
}

以下是两个类的映射:

public class EntityMap : ClassMap<Entity>
{
  public EntityMap()
  {
    Table("Entity")
    Id(x=>x.EntityId).GeneratedBy.Increment();
    *---- How to map for ParentEntities and ChildEntites?----*
  }
}

public class EntityLinksMap : ClassMap<Entity>
{
  public EntityMap()
  {
    Table("Entity")
    Id(x=>x.EntityId).GeneratedBy.Increment();
    References(x=>x.ParentEntityId).Column("ParentEntityId");
    References(x=>x.ChildEntityId).Column("ChildEntityId");
  }
}

我的问题是如何在ParentEntities和ChildEntites的实体类中进行映射,以便获取特定实体的父级和子级列表?

2 个答案:

答案 0 :(得分:0)

我会说,你真的很好,你使用中间人表作为标准的映射实体。

我说的是,我们可以使用映射而不将该配对表表示为映射实体。语法为HasManyToMany

但是因为您已选择将配对表作为实体,您必须更改商业模式:

public class Entity
{
     public virtual int EntityId {get; set}
     //public virtual IList<Entity> ParentEntities {get; set}
     //public virtual IList<Entity> ChildEntities{get; set}
     public virtual IList<EntityLinks> ParentEntityLinks {get; set}
     public virtual IList<EntityLinks> ChildEntityLinks {get; set}
}
然后

映射将是:

public EntityMap()
{
     Table("Entity")
     Id(x=>x.EntityId).GeneratedBy.Increment();

     HasMany(x => x.ParentEntityLinks)...
     HasMany(x => x.ChildEntityLinks)...
}

有关 HasMany 映射的更多信息:

Adam Bar的

Mapping-by-Code - Set and Bag

向下滚动到流利的NHibernate&等效

HasMany(x => x.Users)
    .AsSet<CustomComparer>() // or .AsSet(), .AsBag()
    .Fetch.Join()
    .BatchSize(100)
    .LazyLoad() // or .ExtraLazyLoad()
    .Table("tableName")
    .Schema("schemaName")
    .Cascade.AllDeleteOrphan() // or .None(), .SaveUpdate(), .All(), DeleteOrphan()
    .Inverse()
    ...
    // and many more settings
    ...

事实上,如果你真的想要保留多对多和属性,比如

 public virtual IList<Entity> ParentEntities {get; set}
 public virtual IList<Entity> ChildEntities{get; set}

映射将是

HasManyToMany(x => x.ParentEntities)...
HasManyToMany(x => x.ChildEntities)...

...我不建议使用:How to create NHibernate HasManyToMany relationNhibernate: How to represent Many-To-Many relationships with One-to-Many relationships?以下是如何使用HasManyToMany的更多详细信息...

答案 1 :(得分:0)

我想出了要使用的映射。我只是对手头的问题感到困惑,因为这是自我引用。

public class EntityMap : ClassMap<Entity>
{
  public EntityMap()
  {
    Table("Entity")
    Id(x=>x.EntityId).GeneratedBy.Increment();

   //solution mapping
    HasManyToMany(x => x.ChildEntities)
             .Table("EntityLinks")
             .ParentKeyColumn("ParentEntityId")
             .ChildKeyColumn("ChildEntityId");
        HasManyToMany(x => x.ParentEntities)
             .Table("EntityLinks")
             .ParentKeyColumn("ChildEntityId")
             .ChildKeyColumn("ParentEntityId");

  }
}

public class EntityLinksMap : ClassMap<EntityLinks>
{
  public EntityMap()
  {
    Table("EntityLinks")
    Id(x=>x.EntityId).GeneratedBy.Increment();
    References(x=>x.ParentEntityId).Column("ParentEntityId");
    References(x=>x.ChildEntityId).Column("ChildEntityId");
  }
}