NHibernate: Two foreign keys in the same table against same column

落爺英雄遲暮 提交于 2019-12-25 02:57:49

问题


I am working on a consignment booking software. The consignment object has the following structure:

public class Consignment
{
   //Other properties

   public virtual Station FromStation{get;set;}

   public virtual Station ToStation{get;set;}
}

and here is the station object:

public class Station
{
   public virtual int StationId{get;set;}

   public virtual string StationName{get;set;}

    //I don't want this property but I have to keep it.
    public virtual Iesi.Collections.Generic.ISet<Consginment> ConsginmentFrom
    {
        get;
        set;
    }

    //I don't want this property but I have to keep it here.
    public virtual Iesi.Collections.Generic.ISet<Consginment> ConsginmentTo
    {
        get;
        set;
    }
}

In the database side, there would be a Station table, and the Consignment table would have two int columns (called FromStation and ToStation), storing the ids of the station.

I am not much of an NHibernate guy, after googling and reading for half a day I came up with the following Mapping Files:

Station.hbm.xml

<class name="Station" table="Station">
    <id name="StationId" >
      <column name="STATION_ID" not-null="true" />
      <generator class="identity" />
    </id>
    <property name="StationName" >
      <column name="STATION_NAME" not-null="true"  />
    </property>
    <set name="ConsginmentFrom" inverse="true" lazy="true" generic="true">
      <key>
        <column name="StationId" />
      </key>
      <one-to-many class="Consignment" />
    </set>
    <set name="ConsignmentTo" inverse="true" lazy="false" generic="true">
      <key>
        <column name="StationId" />
      </key>
      <one-to-many class="Consignment" />
    </set>
  </class>

Consignment.hbm.xml

<class name="Consignment" abstract="true"
           table="Consignment" lazy="false">
    <id name="ConsignmentId">
      <generator class="hilo"/>
    </id>

  <!--Column mapping for other properties-->

    <many-to-one name="FromStation" class="Station">
      <column name="STATION_ID" not-null="true" />
    </many-to-one>

    <many-to-one name="ToStation" class="Station">
      <column name="STATION_ID" not-null="true" />
    </many-to-one>


  </class>

But the above is generating strange DB structure. I have to do it xml mapping files as a lot has already been written in xml. Am I doing it correctly? Can there be a better way?

Thanks for reading this.


回答1:


There are a few things I can see wrong with the mappings.

  1. The FromStation and ToStation properties map to the same column in the Consignment table. They should map to different columns such as FROM_STATION_ID and TO_STATION_ID:

    <many-to-one name="FromStation" class="Station">
      <column name="FROM_STATION_ID" not-null="true" />
    </many-to-one>
    
    <many-to-one name="ToStation" class="Station">
      <column name="TO_STATION_ID" not-null="true" />
    </many-to-one>
    
  2. The Set for ConsignmentFrom and ConsignmentTo in Station maps to StationID instead of Station_Id. Also you need to use the FROM_STATION_ID AND TO_STATION_ID as the key columns.

    <set name="ConsignmentFrom" inverse="true" lazy="true" generic="true">
      <key column="FROM_STATION_ID" />
      <one-to-many class="Consignment" />
    </set>
    
    <set name="ConsignmentTo" inverse="true" lazy="false" generic="true">
      <key colum="TO_STATION_ID" />
      <one-to-many class="Consignment" />
    </set>
    

Also consignment is misspelt in some places which may also cause some confusion.



来源:https://stackoverflow.com/questions/15964974/nhibernate-two-foreign-keys-in-the-same-table-against-same-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!