2

I'm having problems getting the right hbm.xml for mapping a Many-to-One relationship over a link table:

  <class name="Car" table="Cars" lazy="true"> 
    <id name="CarKey" type="int">
      <generator class="native" />
    </id>
    [properties]...
    <many-to-one ??? />
  </class>

  <class name="Driver" table="Drivers" lazy="true"> 
    <id name="DriverKey" type="int">
      <generator class="native" />
    </id>
    [properties]...
  </class>

  <class name="CarDriverLink" table="CarDriverLinks" lazy="true"> 
    <id name="CarDriverLinkKey" type="int">
      <generator class="native" />
    </id>
    <property name="CarKey">
      <column name="CarKey" sql-type="int" not-null="true" />
    </property>
    <property name="DriverKey">
      <column name="DriverKey" sql-type="int" not-null="true" />
    </property>
  </class>

Imagining that in this example a car can have only one driver, but a driver can have multiple cars, how would I add a many-to-one relationship onto the Car mapping to allow a Car to see which Driver can drive it, using the CarDriverLinks table?

Marcus Abrahão
  • 696
  • 1
  • 8
  • 18
Phill.Zitt
  • 343
  • 2
  • 16

1 Answers1

0

So for a one-to-many, many-to-one you don't necessarily need a cross reference table in between. That will give you a many-to-many relationship. Since each car will have only one driver, you can add a DriverID to your Car table and class. Your driver can still have multiple cars. I use Fluent NHibernate, so I don't really remember the XML mapping but check out this question that explains the rest of what you're looking for. One To Many, Many To One - NHibernate

Community
  • 1
  • 1
Papa Burgundy
  • 6,397
  • 6
  • 42
  • 48
  • 1
    I understand that I don't necessarily need a cross reference table in between, but I have one, and I need to use it as I cannot go the simple route and add a DriverKey FK to the Car Table. – Phill.Zitt Dec 08 '15 at 22:10