I was wondering if there is a way I can specify the value of a column in a many-to-one relationship in the configuration. I think the best way to explain what I need is by showing an example.
We have a NameValueLists table that is used as a lookup table. This table contains all of our lookup values. It has two unique keys, the primary key (the ID column) and a combination of ListName and NumValue. Any table that has a foreign key to this one stores the NumValue. This means that you need to know which ListName to use in your queries in order to get the correct value from the NameValueLists table.
NameValueLists table:
Code:
ID ListName ValueName NumValue
---------- ------------ ---------------- --------------
1 AddressType Home 1
2 AddressType Work 2
3 AddressType Mailing 3
4 PhoneType Home 1
5 PhoneType Work 2
6 PhoneType Mobile 3
This address table is an example of a table that gets joined to the NamedValueLists table. Note that the AddressType column is a foreign key to the NumValue column in the NameValueLists table. You need to hard code the ListName value in your query to get the correct value.
Address table:
Code:
ID ... AddressType
--------- --------- -------------------
1 ... 2
2 ... 1
3 ... 2
4 ... 3
An example query would be as such:
Code:
select *
from Address a
join NameValueLists vl
on a.AddressType = vl.NumValue AND vl.ListName = 'AddressType'
I am not a big fan of the way this works, but the database is not mine, and I am unable to make changes to it.
So, the question is can I do something like this in my hibernate hbm.xml files (or an equivalent, note the 2nd column has a "value" attribute in it)?:
Code:
<class name="MyAssembly.BusinessObjects.Sale, MyAssembly" table="Address" lazy="false">
<many-to-one name="AddressType" class="MyAssembly.BusinessObjects.NamedValue, MyAssembly">
<column name="Type" property-ref="NumValue"/>
<!-- What would be equivalent to this?: -->
<column value="AddressType" property-ref="ListName"/>
</many-to-one>
...
</class>
There is no value attribute for columns in hibernate, but what I am trying to do here is specify the "AddressType" value explicitly since it does not exist in the Address table.
I hope I have made the problem clear. If more information is needed please let me know. Any help is greatly appreciated.
Thanks.