Fluent nhibernate m-to-m mapping with external table

谁说胖子不能爱 提交于 2019-12-25 03:18:05

问题


I have two tables in Oracle

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

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

Table EntityLinks will store ManyToMany relationship between various entities. ParentEntityId and ChildEntityId are having foreign key relationship with Entity.

I have below a below class for Entity as well

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}
}

Here is the mapping for both the classes:

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");
  }
}

My question is how should I do mapping in entity class for ParentEntities and ChildEntites so that I get the list of both parent and child for a particular entity?


回答1:


I would say that it is really good, that you are using man-in-the-middle table as a standard mapped entity.

I am talking about the fact, that we can use mapping without that pairing table being represented as mapped entity. The syntax would be HasManyToMany

But because you've chosen to have pairing table as an entity you have to change the Business model:

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}
}

Mapping then would be:

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

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

More about HasMany mapping:

Mapping-by-Code - Set and Bag by Adam Bar

Scroll down to Fluent NHibernate's equivalent:

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
    ...

If in fact, you really want to keep the many-to-many, and properties like

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

The mapping will be

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

...which I would not suggest to use: How to create NHibernate HasManyToMany relation or Nhibernate: How to represent Many-To-Many relationships with One-to-Many relationships?, Here are some more details how to use HasManyToMany if ...




回答2:


I figured out the mappings to be use. I was just confused in the problem at hand since this is self-referencing.

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");
  }
}


来源:https://stackoverflow.com/questions/29413690/fluent-nhibernate-m-to-m-mapping-with-external-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!