2012-09-17 2 views
1

다음 개체 구조를 고려하십시오. Nhibernate 2 조건이있는 하위 쿼리 및 부수적 쿼리

Product 
id : int 
name : string 
attribute : list of Attribute 

Attribute 
id : int 
name: string 
value : string 
product_id : int 

질문

은 다음과 같습니다

특성 :

가 동시에 속성이있는 모든 제품을 선택 모든 제품을 다음과 같은 조건 를 반환하는 하위 쿼리를 형성하는 방법을 QueryOver을 사용하여 name = "Color"Value = "Red" 및 속성 이름 = "크기"값 = "XXL"?

편집 : 샘플 SQL :

select * from Product p where 
exists (select id from attribute where name = 'Color' and value = 'Red' and product_id = p.id) 
and 
exists (select id from attribute where name = 'Size' and value = 'XXL' and product_id = p.id) 
+0

이 쿼리를 sql에 어떻게 작성 하시겠습니까? 그건 당신이 SQL에 익숙하지 않은 이상 제가 보통 시작하는 방법입니다. –

+0

각 속성에 하위 쿼리가 있어야합니다. 성능이 좋지는 않을 것입니다. – Roland

+0

@Roland : 예, 원하는 SQL을 작성할 수 있습니까? 그러면 질문을 훨씬 쉽게 대답 할 수 있습니다. –

답변

4

속성이 일치 카운트 하위 쿼리를 사용하여이 IMO 프로퍼티 제품이에있을 경우 하위 쿼리가 단축 할 수

Product productAlias = null 

// get the count of matching Attributes 
var subquery = QueryOver.Of<Product>() 
    .Where(p = > p.Id == productAlias.Id) 
    .JoinQueryOver(p => p.Attributes) 
     .Where(a => (a.Name == "Color" && a.Value == "Red") || (a.Name == "Size" && a.Value == "XXL")) 
    .Select(Projections.RowCount()); 

// get the Products where all match 
var results = session.QueryOver(() => productAlias) 
    .WithSubquery.WhereValue(2).Eq(subquery) 
    .List(); 

가장 쉬운 방법입니다 특성 클래스

+0

나는이 과부하를 볼 수 없다. 그 버전은 무엇입니까? . (2, 서브 쿼리) – Roland

+0

그것은 오타입니다. – Firo