Define NHibernate mapping by code on primary key consisting of multiple columns

1.4k views Asked by At

Does someone have any idea to create a reference on multiple columns using NHibernate mapping only? This mapping only allows one column.

Bag(p => p.Childs,
    map =>
    {
        map.Key(k =>
            {
                k.Column("KeyPart1");
            });
        map.Key(k =>
            {
                k.Column("KeyPart2");
            });
    }
    , ce => ce.OneToMany());

This results in the mapping (stripped of xml tags):

<bag name="Childs">
  <key column="KeyPart1" />
  <one-to-many class="Child" />
</bag>

This results in the error: Result Message: NHibernate.FKUnmatchingColumnsException : Foreign key (FK1C5AAEC658BD05ED:Child [KeyPart2])) must have same number of columns as the referenced primary key (Parent [KeyPart1, KeyPart2])

The child mapping is:

ManyToOne(p => p.Parent, 
    map => map.Columns(
        col1 => 
        {
            col1.Name("KeyPart1");
        },
        col2 =>
        {
            col2.Name("KeyPart2");
        }
        ));

But I think I need this mapping, with both key parts:

<bag name="Childs">
  <key column="KeyPart1" />
  <key column="KeyPart2" />
  <one-to-many class="Child" />
</bag>

This is the complete structure of the classes:

public class ParentIdentifier
{
    public virtual string KeyPart1 { get; set; }
    public virtual string KeyPart2 { get; set; }

    public override bool Equals(object obj)
    {
        if (obj == null)
            return false;
        var t = obj as ParentIdentifier;
        if (t == null)
            return false;
        if (KeyPart1 == t.KeyPart1 && KeyPart2 == t.KeyPart2)
            return true;
        return false;
    }

    public override int GetHashCode()
    {
        return (KeyPart1 + "|" + KeyPart2).GetHashCode();
    }

    public override string ToString()
    {
        return string.Format("KeyPart1 = {0}; KeyPart2 = {1}", KeyPart1, KeyPart2);
    }
}


[Serializable]
public class Parent
{
    #region Primary key

    public virtual ParentIdentifier Id { get; set; }

    #endregion Primary key

    public Parent()
    {
        Childs = new List<Child>();
    }

    public virtual IList<Child> Childs { get; set; }
}

public class ParentMap : ClassMapping<Parent>
{
    public ParentMap()
    {
        Table("Parent");

        ComponentAsId(
            x => x.Id,
            caim =>
            {
                caim.Property(x => x.KeyPart1, pm => 
                    {
                        pm.Column("KeyPart1");
                        pm.Length(20);
                    } );
                caim.Property(x => x.KeyPart2, pm => 
                    {
                        pm.Column("KeyPart2");
                        pm.Length(64);
                    } );
            });

        Bag(p => p.Childs,
            map =>
            {
                map.Key(k =>
                    {
                        k.Column("KeyPart1");
                    });
                map.Key(k =>
                    {
                        k.Column("KeyPart2");
                    });
            }
            , ce => ce.OneToMany());
    }
}

[Serializable]
public class Child
{
    #region Primary key

    public virtual int ChildId { get; set; }

    #endregion Primary key

    public virtual Parent Parent { get; set; }
}


public class ChildMap : ClassMapping<Child>
{
    public ChildMap()
    {
        Table("Child");

        Id(p => p.ChildId, 
            map => 
                {
                    map.Generator(Generators.Assigned);
                    map.Column("ChildId");
                });

        ManyToOne(p => p.Parent, 
            map => map.Columns(
                col1 => 
                {
                    col1.Name("KeyPart1");
                },
                col2 =>
                {
                    col2.Name("KeyPart2");
                }
                ));
    }
}
1

There are 1 answers

0
Ricardo Peres On BEST ANSWER

Because you are calling map.Key(...) twice, the second overrides the first! You should call instead:

map.Key(k =>
        {
            k.Columns(
                c1 => c1.Name("c1"),
                c2 => c2.Name("c2") /*as many as you want*/
            );
        });