如何将此Hibernate属性更改为不需要SQL语句

时间:2011-09-28 20:16:53

标签: java hibernate hibernate-mapping

我正在使用Hibernate dynamic-map在我的应用程序中加载DataEntry对象。当我加载任何给定的DataEntry对象时,我想检索其InformationTypeId列的描述性值。 DataEntry表的InformationTypeId实际上是InformationType表中的代码。我希望Hibernate在映射文件中执行的操作是查找DataEntry对象的InformationType行并返回其InformationDesc列的值。

以下是表格和列:

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>

上述方法适用于加载。它有两个问题:

  1. 更新不起作用
  2. 它需要手动SQL(MS SQL Server)。
  3. 有没有一种方法可以在没有SQL语句的情况下自定义此行为?

    这似乎是相当普遍的事情,但我没有看到它的任何例子。我尝试了以下使用连接,但它不起作用。我相信这个连接不起作用,因为连接尝试使用DataEntry的私钥加入。我需要它使用DataEntry的InformationTypeId加入。

    <join table="informationType" optional="true" fetch="select">
        <key column="InformationTypeId"/>
        <property name="informationType" type="string">
            <column name="InformationDesc"/>
        </property>
    </join>
    

1 个答案:

答案 0 :(得分:0)

只需将InformationType表格映射到InformationType实体,并在DataEntryInformationType之间建立一个热切的多对一关联。