0

我有一个名为类/表“注释”Nhibernate:如何为同一班级的孩子创建两个班级的<bag>?

<class name="Comment"> 
<id name="Id"> 
    <generator class="guid"/> 
</id> 
<property name="ReferenceId" index="Comment_ReferenceId_Index" /> 
<property name="Contents" length="1024" /> 

,我需要在其他几类创建的评论的袋子像合同

<class name="Contract"> 
<id name="Id"> 
    <generator class="guid"/> 
</id> 
<property name="Status"/>  
<bag name="Comments"> 
    <key column="ReferenceId" /> 
    <one-to-many class="Comment" /> 
</bag> 

或应用程序:

<class name="Application"> 
<id name="Id"> 
    <generator class="guid"/> 
</id> 
<property name="Status" /> 
<bag name="Comments"> 
    <key column="ReferenceId" /> 
    <one-to-many class="Comment" /> 
</bag> 

但是这个映射只给我一个外键,我如何创建集合以对几个类进行评论?

+0

不知道我是否理解。 ReferenceId是哪个表的外键? – 2011-12-14 20:05:48

回答

1

答案是:

设置外键的名称为“无”,然后休眠不创建FK但跟踪引用。

<class name="Contract"> 
<id name="Id"> 
    <generator class="guid" /> 
</id> 
<property name="Status" />  
<bag name="Comments"> 
    <key column="ReferenceId" foreign-key="none"/> 
    <one-to-many class="Comment" /> 
</bag> 
0

通过这种映射,您可以获得一个外键,因为您已经以这种方式显式配置它。 <key column="...">与两个父类都是相同的ReferenceId列,这意味着NHibernate试图对ApplicationContract制作ReferenceId外键,但这不能完成。

最简单的方法是给你的关键列不同的名称,如ContractIdApplicationId

如果你想拥有一个ReferenceId列将引用要么ContractApplication并不会同时,你要辨别字段添加到您的Contract并做<any>映射 - see this article