This is a mirror of official site: http://jasper-net.blogspot.com/

NHibernate one-to-one mapping, non-primary keys

| Sunday, May 22, 2011
Can't get NHibernate to generate the correct query. It keeps using the primary keys of the two tables I'm joining for the one-to-one relationship, and I can't figure out how to specify the foreign key in one of the tables.

tableA      tableB
{ aID,      { bID,
  bID,        z,
  c,          y,
  d }         x }

so the tableA should join to tableB using tableA.bID = tableB.bID. How can I specify this in the mapping for tableA? I'm using the tableA class to retrieve a row from tableA and a row from tableB, as it is a real one to one relationship.

NHibernate generates the sql to join the tables using tableA.aID = tableB.bID, which is wrong.

This does not work:

<class name="tableA" table="tableA">
  <id name="aID" column="aID" />
  <property name="bID" column="bID" />
  <property name="c" column="c" />
  <property name="d" column="d" />
  <one-to-one name="otherThing" class="tableB" foreign-key="bID" />
</class>

<class name="tableB" table="tableB">
  <id name="bID" column="bID" />
  <property name="z" column="z" />
  <property name="y" column="y" />
  <property name="x" column="x" />
</class>

Answer:

This is the correct way to map it:

<class name="tableA" table="tableA">
  ...
  <many-to-one name="otherThing" class="tableB" column="bID" unique="true" />
</class>

Read more: Satackoverflow

Posted via email from Jasper-net

0 comments: