关于动态列映射的几行可以在这里找到:NHibernate Dynamic Columns Number。实际上,在这种情况下,这可能就是这种方式。
<join table="ElemntValues" >
<key column="ElementId" />
<dynamic-component>
...
但老实说,我最终得到了一些不同的解决方案。
这是我的 C# 实体(例如联系人),带有一些 MuchMore 字段
IDictionary _muchMore;
// se the property name ... used later for mapping
public virtual IDictionary MuchMore
{
get { return _muchMore?? (_muchMore= new Hashtable()); }
set { _muchMore= value; }
}
现在,NHibernate 的映射可能性不可阻挡
持久实体在运行时不一定必须表示为 POCO 类。 NHibernate 还支持动态模型(在运行时使用字典字典)。使用这种方法,您无需编写持久类,只需映射文件。
...
首先,在映射文件中,entity-name 必须声明为而不是(或除了)类名称
这很酷。因为现在我们可以创建 ContactMuchMore 实体的品牌虚拟映射,这将代表连接的表:
<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" />
难以置信...我们 NHibenrate 确实有映射实体 - 虚拟实体。它依赖于 Contact.. 所以我们可以使用真实的联系人作为 ID 生成器和one-to-one 映射
这里是Contact.cs 映射:
<class name="Contact" ... >
...
// here we mapp the property name
// to our virtual entity
<one-to-one name="MuchMore"
entity-name="ContactMuchMore"
cascade="all" />
一对一有一些阴暗面(两个表总是加载),但可以通过投影来解决。使用 cascade all - 将值持久保存到任何这些表(contact 或 contact_hasmore)中没有问题。
我的回答是描述 xml 映射(不流利)。但我想如果概念清晰,我们可以流畅地使用类似的东西,或者异常使用 xml。