NHibernate映射外键

时间:2009-10-13 14:23:00

标签: nhibernate nhibernate-mapping

我有两张桌子,车辆和制造。使用MakeId作为Vehicle表上的外键将两者连接起来。我的映射文件看起来与此

类似
    <?xml version="1.0" encoding="utf-8" ?>
    <hibernate-mapping  xmlns="urn:nhibernate-mapping-2.2">
      <class name="Demo.Business.Objects.Vehicle, Demo.Business.Objects" table="Vehicle" >
        <id name="VehicleId" type="int" >
          <generator class="native"  />
        </id>
        <property name="RegNumber" type="String" />
        <property name="VehicleId" type="int" />
        <property name="CustomerId" type="int" />

        <join table="Make"  fetch="join">

          <key column="MakeId" foreign-key="MakeId"/>
          <property name="Description"  type="String"  />
        </join>
      </class>
    </hibernate-mapping>

我原以为这会加入make id上的两个表,但是ios生成的SQL会尝试以下连接:vehicle.vehicleid = make.makeid.

我怎样才能让它发挥作用?即我期待:

    select * from Vehicle
    inner join Make on Make.MakeId = Vehicle.Make Id

1 个答案:

答案 0 :(得分:0)

您需要以不同方式映射Make类:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping  xmlns="urn:nhibernate-mapping-2.2">
  <class name="Demo.Business.Objects.Vehicle, Demo.Business.Objects" table="Vehicle" >
    <id name="VehicleId" type="int" >
      <generator class="native"  />
    </id>
    <property name="RegNumber" type="String" />
    <property name="VehicleId" type="int" />
    <property name="CustomerId" type="int" />
    <many-to-one name="Make" column="MakeId"/>
  </class>
  <class name="(blahblah).Make, blahblah">
    <id name="MakeId" type="int">
      <generator class="native" />
    </id>

    <property name="Description"  type="String"  />
  </class>
</hibernate-mapping>

您的“Vehicle”类应具有Make类型的Make属性。