我在获取正确的hbm.xml以通过链接表映射多对一关系时遇到问题:
<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>
假设在这个例子中,一辆车只能有一个司机,但一个司机可以有多辆车,我该如何在汽车映射中添加多对一关系,让汽车使用CarDriverLinks表来查看哪个司机可以驾驶它?
因此,对于一对多、多对一,您不一定需要介于两者之间的交叉引用表。这将给你带来一种多对多的关系。由于每辆车只有一个驾驶员,您可以将DriverID添加到您的car表和类中。您的驾驶员仍然可以拥有多辆车。我使用Fluent NHibernate,所以我真的不记得XML映射了,但看看这个问题,它解释了您要查找的其余内容。一对多,多对一-NHibernate