Fluent nhibernate m-to-m mapping with external table
Asked Answered
P

2

0

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?

Phenylketonuria answered 2/4, 2015 at 13:3 Comment(0)
G
0

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

Gilemette answered 2/4, 2015 at 13:29 Comment(0)
P
0

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");
  }
}
Phenylketonuria answered 6/4, 2015 at 11:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.