首先,我确信必须有一个简单的解决方案,但我找不到它。 (是的,我用Google搜索了它)
如果我运行此标准..
IList<Team> teams = session.CreateCriteria<Team>("t")
.CreateCriteria("t.TeamMembers", "m")
.Add(Expression.Eq("m.Enabled", true))
.List<Team>();
..生成的SQL类似于:
SELECT t.*, m.* FROM Teams t INNER JOIN TeamMembers m ON t.ID = m.TeamID
因为我只需要表Teams
中的列。如何指示NHibernate停止从TeamMembers
中取出未使用的列?
(我真正的实现非常复杂,返回大量数据,所以这是一个简洁的简化示例)
编辑:“已添加:.Add(Expression.Eq(”m.Enabled“,true))”“到条件
这是我使用相同设置进行的测试:
public class Team
{
public virtual int ID { get; private set; }
public virtual Iesi.Collections.Generic.ISet<Member> Members { get; set; }
}
public class Member
{
public virtual int ID { get; private set; }
public virtual bool Enabled { get; set; }
public virtual Team Team { get; set; }
}
<class xmlns="urn:nhibernate-mapping-2.2" name="TestApp.Team, ClassLib" table="`Team`">
<id access="backfield" name="ID" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="ID" />
<generator class="identity" />
</id>
<set lazy="true" name="Members">
<key>
<column name="TeamID" />
</key>
<one-to-many class="TestApp.Member, ClassLib" />
</set>
</class>
<class xmlns="urn:nhibernate-mapping-2.2" name="TestApp.Member, ClassLib" table="`Member`">
<id access="backfield" name="ID" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="ID" />
<generator class="identity" />
</id>
<property name="Enabled" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Enabled" />
</property>
<many-to-one class="TestApp.Team, ClassLib" name="Team">
<column name="TeamID" />
</many-to-one>
</class>
SELECT this_.ID as ID11_1_, m1_.ID as ID10_0_, m1_.Enabled as Enabled10_0_, m1_.TeamID as TeamID10_0_ FROM "Team" this_ inner join "Member" m1_ on this_.ID=m1_.TeamID WHERE m1_.Enabled = @p0;@p0 = True [Type: Boolean (0)]
请注意澄清:我在测试示例中偶然将TeamMembers
命名为Members
。
答案 0 :(得分:1)
更新更新问题
最好在配置
中使成员集合变得懒惰<set name="Members" lazy="true">
然后只在需要时另外设置,如
.SetFetchMode("TeamMembers", FetchMode.Eager)