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