I have a legacy Oracle DB in which most tables have a composite id. The composite id usually consists of a company code and another identifying code. For example, the id for the "M_BANK" table consists of the company code (COMP_CODE) and the bank code (BANK_CODE). For each of these tables, I have defined a separate class to act as the Id, containing the properties that make up the composite id.
In this mapping file, you can see that I have a many-to-one (Currency) mapped using COMP_CODE and CUR_CODE (the elements of the composite key for M_CUR (Currency)).
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" schema="delfour" default-cascade="save-update" auto-import="true">
<class name="MT.Model.Bank, MT" lazy="false" table="M_BANK">
<composite-id
name="Id"
class="MT.Model.BankId, MT">
<key-property name="Company" type="string" column="COMP_CODE"/>
<key-property name="BankCode" type="string" column="BANK_CODE"/>
</composite-id>
.
.
.
<property name="CurrencyCode" type="string" column="CUR_CODE" not-null="true" />
<many-to-one name="Currency" class="MT.Model.Currency, MT" not-null="true" update="false" insert="false" cascade="none">
<column name="COMP_CODE"/>
<column name="CUR_CODE"/>
</many-to-one>
<many-to-one name="Company" class="MT.Model.Company, MT" not-null="true" update="false" insert="false" cascade="none">
<column name="COMP_CODE"/>
</many-to-one>
</class>
However, I have a problem that I have not yet figured out how to resolve. My problem is that in many cases I don't want the COMP_CODE to be used to find the Currency object. Instead, I would like to use a "calculated" value in addition to the CUR_CODE. The reason for this is that multiple companies can be assigned what we call a "Global Company Code". If a company has one, that other value needs to be used to find the Currency object. Unfortunately, it is not stored in M_BANK.COMP_CODE. It is stored in "Bank.Company.GlobalCode".
So, if Bank.Company.GlobalCode has a value, I want to use that value to obtain the related Currency object, otherwise I want to use the CUR_CODE value.
I didn't include the mappings for Company and Currency because I didn't think that would be necessary, but I can post them if needed.
If there is a way to map this relationship, that would of course be my first choice. If not, I would love some suggestions on how best to accomplish this. I was looking into using the IInterceptor.OnPrepareStatement to alter the generated SQL, but I don't appear to have access to the positional parameters at that point, and I would prefer not to have to do something like that if possible.
Any help or advice would be appreciated! Thanks!