2009-02-05 4 views
3

(Criteria API를 사용하여) 두 클래스 사이에 명시된 매핑없이 두 클래스를 조인 할 수 있습니까?NHibernate - 매핑없이 join

두 클래스를 조인하고 두 클래스에서 데이터를 검색해야하지만 매핑 할 수는 없습니다. 내가 외래 키 SomeID 첫 번째 클래스에 있고 기본 키 ID 초만 알고 있습니다.

그들을 결합하는 기준을 만드는 방법은 무엇입니까? 매핑없이 가능합니까?

제발, 도와주세요, 정말 필요하지만 붙어 있습니다. :/

PS

나는 '어떤'매핑을 알고 있지만 나는 SomeID 추천 필드가 있습니다. 조인을 만들 때만 10 개의 필드에 대한 매핑을 만들면 잔인합니다. 다른 해결책이 없다면 해보 겠지만 나는하고 싶지 않습니다.

답변

6

나는 기준 버전을 모르지만, HQL에서 당신은 이런 식으로 작업을 수행 할 수 있습니다 결과 집합은 다음 고객이 반복됩니다 번 [] 객체의 목록이 될 것입니다

select customer, order from Customer customer, Order order 
    where order.CustomerID = customer.Id 
    and customer.Id = 42 

그가 주문한 주문의 수 (많은 주문에 대해 고객이 한 명 있다고 가정).

ordes가 없으면 결과가 비어 있습니다.

+0

Yeap. 나는 그것을 기준으로 사용하고있다. 나는 이런 식으로 뭔가 만들 가능성을 찾고 있어요 : 고객, 고객, 주문 순서, 사용자, 사용자 order.SomeID_1 = customer.Id order.SomeID_2 = user.Id 에서 선택 고객, 주문 I SomeID_xxx 필드에 디자인 타임에 어떤 필드가 들어 있는지 모릅니다. :/ – dariol

+0

좋아, 알았다. HQL은 하나의 옵션 일뿐입니다. – dariol

+0

에 대한 기준 쿼리를 작성할 수 있었습니까? 그렇다면 pls post..adadly 같은 상황에서 메신저 :( – Illuminati

1

엔티티에서 연관성 속성을 정의하고 싶지 않거나 정의 할 수없는 경우 (동적으로 플러그인을로드하는 모듈 식 응용 프로그램과 같이), Fluent API를 사용하여 "가짜"연관을 만들 수 있습니다.

오차드 소스 코드 "ContentItemAlteration"클래스를 참조하십시오. Orchard에서는 ContentItem 엔티티를 다른 테이블에 저장된 ContentPart 레코드와 조인하려고합니다. 각 ContentPart 유형은 모듈에 의해 제공되므로 ContentItem의 소스를 변경하고 각 새 부품 레코드에 대한 연결을 추가하는 것이 어려울 수 있습니다.

/// <summary> 
    /// Add a "fake" column to the automapping record so that the column can be 
    /// referenced when building joins accross content item record tables. 
    /// <typeparam name="TItemRecord">Either ContentItemRecord or ContentItemVersionRecord</typeparam> 
    /// <typeparam name="TPartRecord">A part record (deriving from TItemRecord)</typeparam> 
    /// </summary> 
    class Alteration<TItemRecord, TPartRecord> : IAlteration<TItemRecord> { 
     public void Override(AutoMapping<TItemRecord> mapping) { 

      // public TPartRecord TPartRecord {get;set;} 
      var name = typeof(TPartRecord).Name; 
      var dynamicMethod = new DynamicMethod(name, typeof(TPartRecord), null, typeof(TItemRecord)); 
      var syntheticMethod = new SyntheticMethodInfo(dynamicMethod, typeof(TItemRecord)); 
      var syntheticProperty = new SyntheticPropertyInfo(syntheticMethod); 

      // record => record.TPartRecord 
      var parameter = Expression.Parameter(typeof(TItemRecord), "record"); 
      var syntheticExpression = (Expression<Func<TItemRecord, TPartRecord>>)Expression.Lambda(
       typeof(Func<TItemRecord, TPartRecord>), 
       Expression.Property(parameter, syntheticProperty), 
       parameter); 

      mapping.References(syntheticExpression) 
       .Access.NoOp() 
       .Column("Id") 
       .ForeignKey("none") // prevent foreign key constraint from ContentItem(Version)Record to TPartRecord 
       .Unique() 
       .Not.Insert() 
       .Not.Update() 
       .Cascade.All(); 
     } 
    } 

이 코드는 간단하게 사용할 수의 ContentItem에 "일부"연결을 추가하여 CRITERIAS에 조인

는 여기에 내가 오차드 소스에서 얻은 정확한 코드입니다. 예를 들어, "ProductPartRecord"라는 테이블에 저장되어있는 "ProductPart"라는 부분이 있다면, 가짜 속성 "ProductPartRecord"에 ContentItem을 가입시킬 수 있습니다.

그건 그렇고,이 전술은 또한 가짜 관계의 HasMany 측면에도 적용될 수 있지만 내가 생각하는 Fluent 소스를 사용자 정의해야합니다.