我正在使用Hibernate動態映射在我的應用程序中加載DataEntry對象。當我加載任何給定的DataEntry對象時,我想要檢索其InformationTypeId列的描述性值。 DataEntry表的InformationTypeId實際上是InformationType表中的代碼。我希望Hibernate在映射文件中執行的操作是查找DataEntry對象的InformationType行並返回其InformationDesc列的值。如何將此Hibernate屬性更改爲不需要SQL語句
下面是表和列:我想加載
create table DataEntry (DataEntryID, InformationTypeId)
create table InformationType (InformationTypeId, InformationTypeDesc)
以下hbm.xml文件中做。在informationType屬性中,我使用read屬性指定了列。這會調用特定的SQL查詢。
<class entity-name="DataEntry" table="DataEntry">
<id name="dataEntryId" type="string">
<column name="DataEntryID" length="36" />
<generator class="assigned" />
</id>
<property name="informationType" type="string">
<column name="InformationTypeId"
read="(SELECT TOP 1 InformationType.InformationDesc FROM InformationType WHERE InformationType.InformationTypeId = InformationTypeId)"/>
</property>
</class>
上述方法確實適用於加載。它雖然兩個問題:
- 更新不工作
- 它需要手動SQL(MS SQL服務器)。
有沒有一種方法可以在沒有SQL語句的情況下自定義此行爲?
這似乎是一些相當普遍的事情,但我還沒有看到它的任何例子。我嘗試了以下使用連接,但它不起作用。我相信這個連接不起作用,因爲連接嘗試使用DataEntry的私鑰進行連接。我需要它加入使用DataEntry的InformationTypeId。
<join table="informationType" optional="true" fetch="select">
<key column="InformationTypeId"/>
<property name="informationType" type="string">
<column name="InformationDesc"/>
</property>
</join>
這是否會爲InformationDesc產生一個對象,而不是一個僅用於InformationDesc的字符串? –
是的,但這就是您在數據庫中所擁有的:數據條目引用InformationType中的一行,並具有ID和說明。我猜想相同的信息類型ID可以被許多DataEntries引用,所以你需要一種方法來獲取現有的信息類型並將其附加到DataEntry。 –