NHibernate ReferencesAny + Composite Id

258 views Asked by At

I have a mapped class that references a number of other classes through an interface using ReferencesAny in the mapping. This works fine as long as the other classes have ordinary single columns primary keys. Now I need to reference a new class through the same interface, which have a combined primary key, consisting of two columns.

Is this possible?

Class1 mapping:

public class Class1Map : ClassMap<Class1>
{
  public Class1Map()
  {
    Table("Class1");

    Id(x => x.Id, "Id").GeneratedBy.Native().UnsavedValue(0);
  }
}

Referencing class mapping:

public class RefClassMap : ClassMap<RefClassMap>
{
  public RefClassMap()
  {
    Table("RefClass");

    Id(x => x.Id, "Id").GeneratedBy.Native().UnsavedValue(0);

    ReferencesAny(x => x.Reference)
      .AddMetaValue<Class1>("Class1")
      .EntityTypeColumn("RefType")
      .EntityIdentifierColumn("RefId")
      .EnittyType<int>()
      .Cascade.None();
  }
}

(RefClass.Reference is of type IInterface and Class1 implements IInterface)

This works perfectly, but how do I add Class2 as a possible reference. (Class2 also implements IInterface)

Class2 mapping looks like this:

public class Class2Map : ClassMap<Class2>
{
  public Class2Map()
  {
    Table("Class2");

    CompositeId()
      .KeyReference(x => x.KeyPart1, "KeyPart1")
      .KeyReference(x => x.KeyPart2, "KeyPart2")
      .Mapped();
  }
}

Im not sure this is possible, but maybe by changing the entitytype to string and doing some kind of mapping of the composite key?

Any suggestions?

1

There are 1 answers

0
Nicolai Heilbuth On BEST ANSWER

The lack of answers and my own further research, indicates that this is not possible. Anyway I ended up finding a workaround.