Associations with composit IDs when associated fields are not all part of the IDs

Original Post Ross Beehler's Avatar

Ross Beehler

19 May, 2010 04:10 PM via web

Got a rather unruly and unfortunate situation where I have two tables with composite IDs that have a many-to-many association where not all of the referencing fields belong to the IDs. Hard to explain, so here are example classes that

public class Class1
{

public string A; // part of the compositeID
public string B; // part of the compositeID
public string C; // not part of the compositeID, but is part of the association to Class2
public IList<Class2> Class2List;

}

public class Class2
{

// all compositeID keys
public string B;
public string C;
public string D;

}

public class Class1Map : ClassMap
{

public Class1Map()
{
    CompositeId()
        .KeyProperty(x => x.A)
        .KeyProperty(x => x.B);

    Map(x => x.C);

    // how do i map for Class2List?  
    // The following gives a NHibernate.FKUnmatchingColumnsException:
    HasManyToMany(x => x.Class2List)
        .ParentKeyColumns.Add("B")
        .ParentKeyColumns.Add("C");
}

}

public class Class2Map : ClassMap
{

public Class2Map()
{
    CompositeId()
        .KeyProperty(x => x.B)
        .KeyProperty(x => x.C)
        .KeyProperty(x => x.D);
}

}

Is this possible to map?

  1. Support Staff 2 Posted by Paul Batum on 21 May, 2010 10:48 PM

    Paul Batum's Avatar

    I'm not sure if its possible. Its probably best if you try to find out if NH
    supports this scenario with HBM XML (I suggest you use the nhibernate
    users<http://groups.google.com/group/nhusers/> mailing
    list) and if so to post that here so we can figure out the fluent
    equivalent.

Reply to this discussion

Preview Comments are parsed with Markdown. Help with syntax

Attached Files

    You can attach files up to 10MB

    What is the last month of the year?

    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