I have posted this question in the NHibernate forum a few days ago, but since it's not .Net specific (in my opinioin) I'm posting it here again, hoping I will get more feedback.
Here's what I have: a ResourceString table with an Id (not very meaningful) and a ResourceId (Guid) which forms a secundary key with the field IsoLangCode.
Most entity tables (like 'Model' in the example below) link to this resource table with a XxxResId field.
The following hbm.xml contains a on-to-many mapping for a property called
Descriptions in the Model class.
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="X.Core.Data.Model, X.Core.Data" table="model" lazy="false">
<id name="Name" column="`Name`" type="String">
<generator class="assigned" />
</id>
<property name="DescriptionResId" type="Guid" />
<bag name="[b]Descriptions[/b]" table="ResourceStrings" lazy="false" >
<key column="ResourceId" foreign-key="DescriptionResId" />
<one-to-many class="X.Core.Data.ResourceString, X.Core.Data" />
</bag>
<bag name="Features" table="Model2Feature" lazy="false">
<key column="ModelName" />
<many-to-many class="X.Core.Data.Feature, X.Core.Data" column="FeatureName" />
</bag>
</class>
</hibernate-mapping>
As you can see, I have set 'DescriptionResId' as the
foreign-key. When I run my getter-code I get an exception, though. I've noticed that the generated SQL is OK, except for the where clause parameter. The parameter is assigned the Name of the Model instance (the PK of the Model table) and not the DescriptionResId I asked NHibernate to use.
Looking at the NHibernate source made me suspect that the foreign-key attribute is ignored for one-to-many (and simpel collection mapping) mappings... (N)Hibernate will always use the PK.
Am I correct, or just missing the obvious?
Thanks,
G.