0

我正在开发寄售预订软件。寄售对象具有以下结构:

public class Consignment
{
   //Other properties

   public virtual Station FromStation{get;set;}

   public virtual Station ToStation{get;set;}
}

这是车站对象:

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;
    }
}

在数据库端,会有一个 Station 表,而 Consignment 表将有两个 int 列(称为 FromStation 和 ToStation),存储车站的 id。

我不是一个 NHibernate 的人,在谷歌搜索和阅读半天后,我想出了以下映射文件:

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>

寄售.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>

但上面正在生成奇怪的数据库结构。我必须做 xml 映射文件,因为很多已经用 xml 编写了。我做得对吗?有没有更好的方法?

感谢您阅读本文。

4

1 回答 1

3

我可以看到映射有一些问题。

  1. FromStation和属性映射到表中的ToStation同一列Consignment。它们应该映射到不同的列,例如FROM_STATION_IDTO_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. SetforConsignmentFromConsignmentToinStation映射到而StationID不是Station_Id。您还需要使用FROM_STATION_IDANDTO_STATION_ID作为键列。

    <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>
    

另外,寄售在某些地方拼写错误,这也可能引起一些混乱。

于 2013-04-12T11:03:44.500 回答