NHibernate子查询当关系键为NULL时,Count子查询

时间:2012-04-05 14:56:11

标签: c# linq nhibernate

我不会得到subselect的数量。 我有:

Class A {
    int Id {get;set;}
    ICollection<B> NameBs {get;set;}
}

Class B {
   int Id {get;set;}
   C NameC {get;set;}
}

Class C {
  int Id {get;set;}
}

我的映射看起来像:

<class name="A" table="A">    
    <id name="Id" ...>
        <generator class="native" />
    </id>
    <set name="NameBs" table="B" inverse="true" cascade="all-delete-orphan" batch-size="20">
        <key column="id_A" not-null="true" />
        <one-to-many class="B" />
    </set>
</class>

<class name="B" table="B">    
    <id name="Id" ...>
        <generator class="native" />
    </id>
    <many-to-one name="NameC" column ="id_C"/>
</class>

<class name="C" table="C">    
    <id name="Id" ...>
        <generator class="native" />
    </id>
</class>

在C#代码中选择是:

var results = (from a in aRep.QueryAll()
               select new myObject{
                                     Id = b.Id,
                                     Count = a.NameBs.Count(b=>b.C == null)
                                   }).ToList();

SQL Select是:

SELECT Id,
       (SELECT cast(count(* ) as INT)
        FROM B b_, C c_
        WHERE  b_.id_c = c_.id AND b_.id_a = a_.id AND (b_.id_c is null))
FROM A a_

但我需要这个选择:

SELECT Id,
       (SELECT cast(count(* ) as INT)
        FROM B b_
        WHERE b_.id_a = a_.id AND (b_.id_c is null))
FROM A a_

任何人都可以帮助我吗?

我尝试在多对一属性中进行映射,如: fetch =“join”,但不起作用。

<class name="B" table="B">    
    <id name="Id" ...>
        <generator class="native" />
    </id>
    <many-to-one name="NameC" column ="id_C" fetch="join"/>
</class>

0 个答案:

没有答案