![](/img/trans.png)
[英]Single table inheritance strategy (or table per class hierarchy) with Oracle and Hibernate
[英]hibernate fetch strategy when using one table per class hierarchy inheritance
您好我正在使用每個類層次結構繼承映射到遺留數據的一個表,如此處所述
這是我的映射大致看起來像
<class abstract="true" name="note" table="NOTES">
<id name="id" type="long" column="NOTE_ID">
<generator class="native"/>
</id>
<discriminator column="NOTE_TYPE" type="string"/>
<property name="orderId" column="ORDER_ID"/>
<property name="text" column="TEXT"/>
<subclass name="PurchaseNote" discriminator-value="PUR" />
<subclass name="CancelNote" discriminator-value="CAN" />
<subclass name="RefundNote" discriminator-value="REF" />
</class>
每個訂單ID可以有許多子類的注釋
Notes被描述為Order Class中的關聯
<class name="order" table="ORDERS">
<id name="id" type="long" column="ORDER_ID">
<generator class="native"/>
</id>
.....
<set name="purchaseNotes">
<key column="orderId" />
<one-to-many class="PurchaseNote"/>
</set>
<set name="cancelNotes">
<key column="orderId" />
<one-to-many class="cancelNote"/>
</set>
<set name="refundNotes">
<key column="orderId" />
<one-to-many class="refundNote"/>
</set>
</class>
如果我執行以下操作,我可以獲取由hibernate提取的關聯集合。
1)在xml的順序中使用set定義中的“Where”子句或2)在notes xml中使用force =“true”作為鑒別器定義的一部分
我也可以在DAO調用會話工廠的hibernate會話中使用HQL“來自RefundNote,其中orderId =?” 或者“注意注意,其中note.class = RefundNote和orderId =?” 獲取正確的RefundNote子類列表,並使用refundNotes上的setter填充Order類中的列表。
所有標准的hibernate東西?
這些方法中的任何一個都將創建集合,向同一個表觸發3個查詢(每個集合一個)。 隨着子類數量的增長,這似乎是無效的......?
我已經閱讀了很多但是看不到任何方式hibernate(通過獲取策略,加入,子選擇等)可以最小化這些調用單個db調用並仍然填充我的集合.....?
我可以通過class.simpleName檢查獲取所有注釋並在Java中迭代以構建集合,但是想知道我是否遺漏了hibernate會做什么.....?
謝謝你的建議
我必須深入研究代碼來驗證,但我懷疑這只是Hibernate如何處理這樣的映射關聯 - 它總是會為每個水合關聯進行一次數據庫調用。
另一種方法是使用一個映射的hibernate集合,Notes,然后為refundNotes等提供瞬態字段。
在針對特定類型的getter中,迭代notes集合以查找感興趣的特定注釋。 在你的setter中,將它添加到一般注釋集合中。
這樣,hibernate將使單個查詢獲取注釋,並且您可以根據需要對水合對象進行操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.