问题
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.
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>
The
Set
forConsignmentFrom
andConsignmentTo
inStation
maps toStationID
instead ofStation_Id
. Also you need to use theFROM_STATION_ID
ANDTO_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