2013-04-12 2 views
0

위탁 예약 소프트웨어로 작업하고 있습니다. 위탁 객체는 다음과 같은 구조가 있습니다NHibernate : 동일한 테이블에 대해 동일한 열에 대해 두 개의 외래 키가 있습니다

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

, 스테이션 테이블이있을 것, 그리고 위탁 테이블은 두 개의 지능 것 열 (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> 

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로 매핑해야합니다. 제대로하고 있습니까? 더 좋은 방법이있을 수 있습니까?

감사합니다.

+0

생성 된 DB 구조의 스키마는 무엇입니까? – mickfold

답변

3

매핑에 문제가 있음을 알 수 있습니다.

  1. FromStationToStation 및 특성은 표 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. ConsignmentFromSetConsignmentToStation의지도를 StationID 대신 Station_Id에. 또한 키 열로 FROM_STATION_IDTO_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> 
    

또한 위탁도 약간의 혼동을 일으킬 수있는 몇 가지 장소에서의 철자가 틀리된다.

+0

잘못 송하하신 것에 대해 죄송합니다 :) 나는 당신이 제공 한 코드를 시험해 보았습니다. 하지만 이제는 CONSIGNMENT 테이블에 STATION_ID 컬럼을 생성하고있다. FROM_STATION_ID 및 TO_STATION_ID와는 다른 것입니다. – James

+0

위의 두 번째 사항은 위탁 테이블에 Station_Id 열을 만드는 것입니다. – James

+0

@James 미안 메모리에서 매핑을 했어. 업데이트 된 답변을 확인하십시오. – mickfold