Few lines about dynamic column mapping could be found here: NHibernate Dynamic Columns Number. And really in this scenario this could be the way.
<join table="ElemntValues" >
<key column="ElementId" />
<dynamic-component>
...
But honestly, I ended up with a bit different solution.
So this is my C# entity (e.g. Contact) with some MuchMore
fields
IDictionary _muchMore;
// se the property name ... used later for mapping
public virtual IDictionary MuchMore
{
get { return _muchMore?? (_muchMore= new Hashtable()); }
set { _muchMore= value; }
}
Now, there is unstopable amount of mapping possiblities with NHibernate
Persistent entities don't necessarily have to be represented as POCO classes at runtime. NHibernate also supports dynamic models (using Dictionaries of Dictionarys at runtime) . With this approach, you don't write persistent classes, only mapping files.
...
First, in the mapping file, an entity-name
has to be declared instead of (or in addition to) a class name
That's cool. Becuase now we can create brand virtual mapping of ContactMuchMore entity, which will reperesent the joined table:
<class entity-name="ContactMuchMore" table="Contact_MuchMore_table"
dynamic-insert="true" dynamic-update="true" batch-size="25" >
<id name="ID" column="Contact_ID" type="int">
<generator class="foreign">
<param name="property">Parent</param>
</generator>
</id>
<one-to-one name="Parent" class="Contact" constrained="true" />
Unbelievable... we with NHibenrate do have mapped entity - virtual one. It has dependency on Contact.. so we can use the real contact as an ID generator and one-to-one
mapping
And here is the Contact.cs
mapping:
<class name="Contact" ... >
...
// here we mapp the property name
// to our virtual entity
<one-to-one name="MuchMore"
entity-name="ContactMuchMore"
cascade="all" />
There are some dark sides of one-to-one (both tables are loaded always) but could be solved with projections. With cascade all - no issues with persisting values into any of these tables (contact or contact_hasmore).
My answer is describing xml mapping (not fluent). But I guess that if the concept is clear, we can use similar stuff in fluent, or exceptionally - use xml.