I have managed to achieve what I want, but I am
very dissatisfied with it. What I ended up doing in my mapping was:
Code:
<class name="ResourceCost" table="STAFF_COST"...
<id ...
<property name="startDate" ...
.
.
.
<component name="costPerHour" ...
<component name="baseMoney" ...
<property name="amount" ...
<property name="currencyCode" formula="( Select res.home_Currency_Code from resource res where res.resource_id = resource_Id )" />
</component>
.
.
.
</component>
</class>
What this ends up as in terms of sql generated by Hibernate is
Code:
select STAFF_COST.START_DATE, ... , ( Select res.home_Currency_Code from resource res where res.resource_id = STAFF_COST.resource_Id ) from STAFF_COST WHERE STAFF_COST.RESOURCE_ID = ...
This is the most
ugly sql imaginable as it performs the subselect for every row returned. My objective was to end up with a nice clean inner join of the form:
Code:
select STAFF_COST.START_DATE, ... , RES.HOME_CURRENCY_CODE from STAFF_COST INNER JOIN RESOURCE RES ON STAFF_COST.RESOURCE_ID = RES.RESOURCE_ID AND STAFF_COST.RESOURCE_ID = ...
I can't see how I can do that with any of <property>, <one-to-one>, <many-to-one> or anything else. Given Hibernate's much touted alignment with standard relational mappings, there has to be a way of assembling data for one class from a simple join of two tables. Doesn't there?
Thanks, Andrew