1
votes

I am trying to load columns from a second table in the database as properties on a model using NHibernate. I'm using an XML mapping file and tried using the join tag. When I build and try to run the code, I get the following error:

The element 'join' in namespace 'urn:nhibernate-mapping-2.2' has invalid child element 'key' in namespace 'urn:nhibernate-mapping-2.2'. List of possible elements expected: 'property, many-to-one, component, dynamic-component, any, map, set, list, bag, idbag, array, primitive-array, sql-insert, sql-update, sql-delete' in namespace 'urn:nhibernate-mapping-2.2'.

The following is a snippet of the mapping file:

<class name="Article" table="article">
    <id name="Id" column="articleID" unsaved-value="-1" type="Int32">
        <generator class="identity" />
    </id>
    <property name="ClientId" column="accountID" type="Int32" />
    <property name="PublicNumber" column="articleNum" />    
    <join table="articleLanguage">
        <key column="accountID" />
        <key column="articleNum" />
        <property name="Question" />
        <property name="LanguageId"/>
    </join>
</class>

What am I doing wrong?

1

1 Answers

1
votes

This aproach, would be working only if the root entity (Article) would have composite key:

<class name="Article" table="article">
    // not and <id> mapping
    <!--<id name="Id" column="articleID" unsaved-value="-1" type="Int32">
        <generator class="identity" />
    </id>-->
    // but <composite-id>
    <composite-id>
      <key-property name="Id" column="articleID" ></key-property>
      <key-property name="PublicNumber" column="articleNum"></key-property>
    </composite-id>

    <property name="ClientId" column="accountID" type="Int32" />
    <!-- <property name="PublicNumber" column="articleNum" /> -->
    <join table="articleLanguage"> 
        <!-- key is represented as ONE key with two columns
        <!--
          <key column="accountID" />
          <key column="articleNum" />
        -->
        <key >
          <column name="accountID"></column>
          <column name="articleNum"></column>
        </key>
        <property name="Question" />
        <property name="LanguageId"/>
    </join>
</class>

I can understand that this is not what you prefer. But, if there are two columns representing relation, there is simply no other way. Because to be able to find exactly one row to join... we need both keys.