是否可以使用NHibernate类型鉴别器作为外键的一部分

时间:2010-12-27 13:29:17

标签: nhibernate

采用以下对象模型:

public abstract class Entity
{
    public Guid Id { get; set; }
}

public class Category : Entity
{
    public string Name { get; set; }

    public ICollection<LocalizedProperty> LocalizedProperties { get; set; }
}

public class Product : Entity
{
    public string Name { get; set; }

    public ICollection<LocalizedProperty> LocalizedProperties { get; set; }
}

public class LocalizedProperty : Entity
{
    public string CultureName { get; set; }
    public string PropertyName { get; set; }
    public string PropertyValue { get; set; }
}

是否可以将类型鉴别器与实体的Id一起用作外键。我们的想法是生成的LocalizedProperties表将是:

LocalizedProperties
-------------------
Id
EntityType
EntityId
CultureName
PropertyName
PropertyValue

我知道这可以使用Table-per-subclass映射,其中每个“Localized”实体都从一个基本的本地化实体类继承,而后者又与LocalizedProperty有关联。但是,如果可能的话,我宁愿没有这种额外的继承级别。

谢谢, 本

更新

感谢Diego提供使用confORM的解决方案。对于那些使用传统映射文件的人,我已经从http://fabiomaulo.blogspot.com/2010/11/conform-any-to-many.html

转换了示例
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" namespace="ConfOrm.UsageExamples.CreateXmlMappingsInBinFolder" assembly="ConfOrm.UsageExamples" xmlns="urn:nhibernate-mapping-2.2">
  <class name="Blog">
    <id name="Id" type="Guid">
      <generator class="guid.comb" />
    </id>
    <property name="Title" />
    <property name="Subtitle" />
    <set name="Tags" cascade="all" where="TagedItemClass = 'ConfOrm.UsageExamples.CreateXmlMappingsInBinFolder.Blog'">
      <key column="TagedItemId" foreign-key="none" />
      <one-to-many class="Tag" />
    </set>
  </class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" namespace="ConfOrm.UsageExamples.CreateXmlMappingsInBinFolder" assembly="ConfOrm.UsageExamples" xmlns="urn:nhibernate-mapping-2.2">
  <class name="Tag">
    <id name="Id" type="Guid">
      <generator class="guid.comb" />
    </id>
    <property name="Name" />
    <any id-type="Guid" name="TagedItem">
      <column name="TagedItemClass" />
      <column name="TagedItemId" />
    </any>
  </class>
</hibernate-mapping>

1 个答案:

答案 0 :(得分:1)

您可以使用<any>

http://nhibernate.info/doc/nh/en/index.html#mapping-types-anymapping

有关完整示例,请检查http://fabiomaulo.blogspot.com/2010/11/conform-any-to-many.html。我认为这正是你所需要的。