问题描述
我正在使用寄售预订软件。
委托对象具有以下结构:
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;}
}
这里是车站对象:
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表,并且寄存表将有两个int列(称为FromStation和ToStation),存储工作站的ID。
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.
我不是一个NHibernate的家伙,阅读半天后,我想出了以下映射文件:
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
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
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>
但是上面是生成奇怪的DB结构。
我必须做它xml映射文件作为很多已经写在xml。
我做的正确吗?
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?
感谢您阅读这篇文章。
推荐答案
-
FromStation code>和
ToStation
属性映射到委托
表中的同一列。它们应映射到不同的列,例如FROM_STATION_ID
和TO_STATION_ID
:
The
FromStation
andToStation
properties map to the same column in theConsignment
table. They should map to different columns such asFROM_STATION_ID
andTO_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>
Set
c $ c>
映射到
StationID
,而不是 Station_Id
。此外,您还需要使用 FROM_STATION_ID
AND TO_STATION_ID
作为键列。
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>
也会造成一些混乱。
这篇关于NHibernate:两个外键在同一个表中对同一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!