2011-08-16 3 views
2

상황은 다음과 같다 : 1. 제품은 많은 카테고리에 속하며, 2. 많은 제품들이있다.many-to-many를 가진 NHibernate 질의

class Product 
{ 
    public int Id { get; set; } 
    public List<Category> Categories { get; set; } 
} 

class Category 
{ 
    public int Id { get; set; } 
    public List<Product> Products { get; set; } 
} 

어떻게 그들 각각의 queryover 함께 할 수있는 방법 ID = 2, 3, 4와 범주에 속하는 모든 제품을 가지고합니까? 나는 동적으로 생성 된 HQL 사용하는 순간 :

select p from Product p where 
exists (select c.id from p.Categories c where c.Id = 2) 
and exists (select c.id from p.Categories c where c.Id = 3) 
and exists (select c.id from p.Categories c where c.Id = 4) 

그리고 매핑은 다음과 같습니다

public class ProductMap : ClassMap<Product> 
{ 
    public ProductMap() 
    { 
     Id(x => x.Id); 
     HasManyToMany(x => x.Categories) 
      .Table("product_category") 
      .ParentKeyColumn("product_id") 
      .ChildKeyColumn("category_id"); 
    } 
} 

public class CategoryMap : ClassMap<Category> 
{ 
    public CategoryMap() 
    { 
     Id(x => x.Id); 
     HasManyToMany(x => x.Products) 
      .Table("product_category") 
      .ParentKeyColumn("category_id") 
      .ChildKeyColumn("product_id"); 
    } 
} 

답변

0

이 시도, 어쩌면 도움의 :

Category categoryAlias = null; 
session.QueryOver<Product>() 
    .JoinAlias(product => product.Categories,() => categoryAlias) 
    .WhereRestrictionOn(() => categoryAlias.Id).IsIn(new [] { 2, 3, 4 }) 
    .List(); 
관련 문제