2014-07-15 1 views
0

2 개의 테이블을 결합하고 싶습니다. 그들은 외래 키를 가지고 있지 않지만 동일한 내용을 가진 컬럼을가집니다. 스키마를 변경할 수 없으며 여기에 간단한 예가 나와 있습니다.nhibernate가있는 외래 키가없는 2 개의 테이블과 코드로 매핑하기

public class Person{ 
    public virtual string Name{get;set;} 
    public virtual int Id{get;set;} 
    public virtual char Gender{get;set;} #this one I want to connect 
} 

public class PersonMapping : ClassMapping<Person>{ 
    public PersonMapping() 
    { 
    Id(x=>x.Id); 
    Property(x=>x.Name); 
    Property(x=>x.Gender); 
    } 
} 

public class Gender{ 
    public virtual char ShortName{get;set;} #to this. So they are the same. When They are connected I want to access the Other properties of this class 
    public virtual int Id{get;set;} 
    public virtual string LongName{get;set;} 
} 
public class GenderMapping: ClassMapping<Gender>{ 
    public GenderMapping() 
    { 
    Id(x=>x.Id); 
    Property(x=>x.ShortName); 
    Property(x=>x.LongName); 
    } 
} 

이제 모든 사람이 해당 성별을 갖고 싶어합니다. 그래서 나는 긴 이름을 찾을 수 있습니다. 나는 해결책이 this과 같다고 생각하지만 유창한 nhibernate를위한 것이다. 나는 또한 criterias 또는 이와 유사한 일을하는 것이 좋을 것입니다.

답변

0

거의 다 왔어. 그러나 속성 성별은 many-to-one으로 매핑되어야하며 property-ref이라는 기능을 사용해야합니다. 참조 : 외래 키가 아닌 연관된 테이블의 유일 키를 참조

  • 5.1.10. many-to-one

    합니다 (property-ref 속성 만 리거시 데이터를 매핑하는데 만 사용되어야한다

: 인용 기본 키. 이것은 추한 관계형 모델입니다. 예를 들어 Product 클래스에 기본 키가 아닌 고유 한 일련 번호가 있다고 가정합니다. (unique 속성은 SchemaExport 도구로 NHibernate에의 DDL 생성을 제어한다.)

<property name="serialNumber" unique="true" type="string" column="SERIAL_NUMBER"/>

그런 다음 OrderItem에 대한 매핑은 다음을 사용할 수 있습니다

<many-to-one name="product" property-ref="serialNumber" column="PRODUCT_SERIAL_NUMBER"/>

는 첫째, 우리는 기준을 도입해야 char 대신 :

public class Person 
{ 
    public virtual string Name{get;set;} 
    public virtual int Id{get;set;} 
    //public virtual char Gender{get;set;} #this one I want to connect 
    public virtual Gender Gedner { get; set; } 
} 

그래서 매핑은 다음과 같이해야한다 : 아담 바로

+0

나는 이것이 내가 무엇이라고 생각 : 코드에 의해 매핑의 아주 좋은 설명이 있습니다

public class PersonMapping : ClassMapping<Person> { public PersonMapping() { Id(x=>x.Id); Property(x=>x.Name); //Property(x=>x.Gender); ManyToOne(x => x.Gender, x => { x.PropertyRef("ShortName"); // this is the property of a Gender class! x.Column("Gender"); // to be mapped with the Pesons's column Gender }); } } 

검색하지만 작동하지 않습니다. 그는 .... (Column)에서 참조 된 엔터티 'System.String'을 찾을 수 없습니다. 나는이 강령을 위해 무엇을 넣을 지 모른다. 그래서 예를 들어 그 참조를위한 전체 젠더 객체를 원한다면 어떻게 할 수 있습니까? – Iron

+0

와우 죄송합니다. ** ShortName ** –

+0

네, 시도 했어. 그는 짧은 이름을 찾을 수 없습니다 – Iron

관련 문제