2011-09-28 68 views
0

我正在使用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> 

上述方法确实适用于加载。它虽然两个问题:

  1. 更新不工作
  2. 它需要手动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> 

回答

0

InformationType表只是映射到一个InformationType实体,并有DataEntryInformationType之间的预先抓取许多-to-one关联。

+0

这是否会为InformationDesc产生一个对象,而不是一个仅用于InformationDesc的字符串? –

+0

是的,但这就是您在数据库中所拥有的:数据条目引用InformationType中的一行,并具有ID和说明。我猜想相同的信息类型ID可以被许多DataEntries引用,所以你需要一种方法来获取现有的信息类型并将其附加到DataEntry。 –