2009-03-03 125 views
0

對於我當前的項目,我必須使用休眠映射舊數據庫,但我遇到了一些問題。 數據庫使用一個「實體」表進行設置,該表包含所有域對象的公共屬性。屬性包括(除其他外)創建日期,所有者(用戶)以及隨後在表對象中使用的主鍵。傳統的休眠映射

上下文的簡單表示是這樣的:

table entity 
- int id 
- varchar owner 

table account 
- int accountid (references entity.id) 

table contact 
- int contactid (references entity.id) 
- int accountid (references account.accountid) 

我的問題表現出自己,當我嘗試集合映射添加到我的帳戶映射,包含屬於該帳戶的所有聯繫人。我嘗試歸結爲以下幾點:

<hibernate-mapping> 
    <class name="Contact" table="entity"> 
     <id name="id" column="id"> 
      <generator class="native" /> 
     </id> 
     <join table="contact"> 
      <key column="contactid"/> 
      <!-- more stuff --> 
     </join> 
    </class> 
</hibernate-mapping> 

<hibernate-mapping> 
    <class name="Account" table="entity"> 
     <id name="id" column="id"> 
      <generator class="native" /> 
     </id> 


     <bag name="contacts" table="contact"> 
      <key column="accountid" /> 
      <one-to-many class="Contact"/> 
     </bag> 

     <join table="account"> 
      <key column="accountid"/> 
      <!-- more stuff --> 
     </join> 

    </class> 
</hibernate-mapping> 

然而,當我嘗試獲取我得到一個SQL錯誤的帳戶,說明該實體表不包含一個名爲ACCOUNTID列。我明白了爲什麼會發生這種情況:當我希望它在聯繫人表中查找時,映射會嘗試在實體表中查找合適的列。我在這裏錯過了一些明顯的東西,還是應該從另一個方向來解決這個問題?

回答

3

這對我來說就像你實際上需要使用Table Per Subclass範式映射繼承一樣。

事情是這樣的:

<class name="entity" table="entity"> 
    <id name="id" column="id"> 
     ... 
    </id> 

    <joined-subclass name="contact" table="contact"> 
     <key column="contactid"/> 
    </joined-subclass> 

    <joined-subclass name="account" table="account"> 
     <key column="accountid"/> 
    </joined-subclass> 
</class> 

這是近似的方式 - 它的詳細Hibernate文檔的第9.1.2節中描述(以防萬一你找不到它,這就是所謂的「表每子類「)。

乾杯

豐富

+0

遺產繼承部分全部ACK。 – boutta 2009-03-03 11:50:09