Many to Many Mapping on a single class

Original Post nabeelfarid's Avatar

nabeelfarid

07 Jun, 2010 10:41 AM via web

Hi Guys

I have come across a situation.

I have two tables Node and NodeValidParents as follows

Node

NodeId_PK
Name

NodeValidParents NodeId_FK (Foreign key from Node)
ValidParentNodeId_FK (Foreign key from Node)

A Node can have a list of ValidParents. As you can see from the tables, a ValidParent is also a node itself. So there is actually a many to many relationship ( A node can have many ValidParents and a ValidParent can belong to many nodes)

My Node class looks like this

public class Node
{
    public virtual int Id
    {
        get;
        set;
    }

    public virtual string Name
    {
        get;
        set;
    }

    public virtual IList<Node> ValidParents
    {
        get;
        protected set;
    }

    public Node()
    {
        ValidParents = new List<Node>();
    }

}

I don't have any class to represent ValidParent because I think there is no need for it, as ValidParent is itself a Node.

I am using autoMapping and I just can't figure out how to map this class to these two tables. I am implementing IAutoMappingOverride for overriding automappings for Node but I am not sure what to do in it.

public class NodeMap : IAutoMappingOverride

{
    public void Override(AutoMapping<Node> mapping)
    {
        //mapping.HasMany(x => x.ValidParents).Cascade.All().Table("NodeValidParents");
    }
}

Could anybody out there help me with this please?

Also I want to make sure that the combination of NodeId_FK and ValidParentNodeId_FK is always unique or Composite Key. I have seem people saying using composite keys is not the best practice and surrogate key should be used. I have no problem with using Surrogate key but I want to make sure that if a node already has a ValidParent , it can not be inserted again. How can i achieve this with fluent nhibernate mapping?

Just so that you know I have never used NHibernate with XML mapping. I have only used Fluent NHibernate.

Thanks
Nabeel

    Reply to this discussion

    Preview Comments are parsed with Markdown. Help with syntax

    Attached Files

      You can attach files up to 10MB

      What number comes after 20?

      Recent Discussions

      05 Jul, 2010 10:29 PM
      05 Jul, 2010 12:45 PM
      05 Jul, 2010 12:42 PM
      05 Jul, 2010 12:17 PM
      05 Jul, 2010 12:12 PM

       

      03 Jul, 2010 12:26 AM
      02 Jul, 2010 02:17 PM
      02 Jul, 2010 08:18 AM
      02 Jul, 2010 12:20 AM
      01 Jul, 2010 10:14 PM