I am using Hibernate dynamic-map to load DataEntry objects in my application. When I load any given DataEntry object, I want to retrieve the descriptive value of its InformationTypeId column. The DataEntry table's InformationTypeId is actually a code into the InformationType table. What I want Hibernate to do in the mapping file is to look-up the InformationType row for the DataEntry object and return the value of its InformationDesc column.
Here are the tables and columns:
create table DataEntry (DataEntryID, InformationTypeId)
create table InformationType (InformationTypeId, InformationTypeDesc)
The following hbm.xml file does what I would like for loading. In the informationType property, I specified the column using the read attribute. This invokes that specific SQL query.
<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>
The approach above does work for loading. It has two problems though:
- Updates don't work
- It requires manual SQL (MS SQL Server).
Is there a way I can customize this behavior without the SQL statement?
This seems like something that would be fairly common, but I've not seen any examples of it. I have tried the following using a join, but it does not work. I believe that this join does not work because the join i开发者_Go百科s trying to join using DataEntry's private key. I need it to join using DataEntry's InformationTypeId.
<join table="informationType" optional="true" fetch="select">
<key column="InformationTypeId"/>
<property name="informationType" type="string">
<column name="InformationDesc"/>
</property>
</join>
Just map the InformationType
table to an InformationType
entity, and have an eagerly fetched many-to-one association between DataEntry
and InformationType
.
精彩评论