2010-10-05 46 views
1

当我们想用一个往返来热切加载实体及其子实体时,推荐使用CreateMultiQuery - 由于我们没有X * Y * Z * T而是X,X * Y,X * Z,X * T。这对我来说工作得很好,但它不是最优的,就好像X是一个有许多列的表一样,我从数据库中提取大量数据。例如:进一步优化NHibernate的CreateMultiQuery用法用于提前取用

const string query1 =“select f from Feature f where f.Id in(:fids)”; const string query2 =“select f from Feature f left join fetch f.SprintAssignment where f.Id in(:fids)”; const string query3 =“select f from Feature f left join fetch f.Tasks where f.Id in(:fids)”; const string query4 =“select f from Feature f left join fetch f.Tags where f.Id in(:fids)”; const string query5 =“select f from Feature f inner join fetch f.Project where f.Id in(:fids)”;

现在,功能表是相当大的(大约20个字段,包括FK到其他表)。这意味着对于query2-5,我获得了数据的大量复制(整个特征字段+连接表字段)。

显然,我们需要FeatureId,因此Identity Map可以解析所有东西(整个Feature对象及其所有子实体),但为什么它会解决其余问题?

有没有一种方法来优化它,所以它只会返回FeatureId +连接表列?

回答

0

没有,没有

0

您必须对您的查询中使用投影和查询根据id投影连接表。这样你只能得到连接表的列。这里使用vb.net中的条件的一个小样本

' create the first query, and restrict it to ids 
Dim dFilterTask As NHibernate.Criterion.DetachedCriteria = Nothing 
dFilterTask = NHibernate.Criterion.DetachedCriteria.For(GetType(Task)) 
' add your criterias here 
dFilterTask.Add(your criterias here) 
dFilterTask.SetProjection(NHibernate.Criterion.Projections.Id) 

' second query, select where the task id is in your first query 
Dim dFilterTaskJoin As NHibernate.Criterion.DetachedCriteria = Nothing 
dFilterTaskJoin = NHibernate.Criterion.DetachedCriteria.For(GetType(TaskJoinedClass)) 
dFilterTaskJoin.Add(NHibernate.Criterion.Subqueries.PropertyIn("TaskId", dFilterTask))