Associations with composit IDs when associated fields are not all part of the IDs
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?
Support Staff 2 Posted by Paul Batum on 21 May, 2010 10:48 PM
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.