我正在创建一个调查应用程序,其中有一个调查,其中包含一组页面。每个页面都有一组问题,每个问题都会有一组答案选项。我的阶级结构是这样的:使用NHibernate有效检索具有嵌套集合的对象
public class Survey : Entity {
public IList<Page> Pages { get; set; }
}
public class Page : Entity {
public IList<Question> Questions { get;set; }
}
public class Question : Entity {
public IList<Option> Options { get; set; }
}
public class Option : Entity {}
每个类的映射是:
<!-- mapping for ID and other properties excluded -->
<class name="Survey">
<bag name="Pages" generic="true" inverse="true">
<key column="SurveyId" />
<one-to-many class="Page" />
</bag>
<bag name="Questions" access="none">
<key column="SurveyId" />
<one-to-many class="Question" />
</bag>
</class>
<class name="Page">
<many-to-one name="Survey" column="SurveyId" />
<bag name="Questions" generic="true" inverse="true">
<key column="PageId" />
<one-to-many class="Question" />
</bag>
</class>
<class name="Question">
<many-to-one name="Page" column="PageId" />
<many-to-one name="Survey" column="SurveyId" />
<bag name="Options" generic="true" inverse="true">
<key column="QuestionId" />
<one-to-many class="Option" />
</bag>
</class>
<class name="AnswerOption">
<many-to-one name="Question" column="QuestionId" />
</class>
我需要显示通过页面的页面,所以我开始与调查对象,并循环上的所有问题,项目和选项。这导致NHibernate执行许多查询,我想优化这个。如何在不执行太多查询的情况下以最好的方式获得嵌套集合的调查对象?
这是我此刻的代码,但它仍然执行许多查询:
var result = Session.CreateMultiQuery()
.Add(Session.CreateQuery("from Survey s inner join fetch s.Pages where s.Id = :id"))
.Add(Session.CreateQuery("from Survey s inner join fetch s.Question where s.Id = :id"))
.SetInt32("id", id)
.List();
IList list = (IList)result[0];
return list[0] as Survey;
我也曾尝试以后的查询,但他们不帮助减少查询的数量。
任何想法?
我已经将lazy =“false”添加到我的映射中,但没有获取并且没有帮助。添加访存减少了查询。但是,我不想更改映射,因为我只会提取一个问题或调查对象,而不需要集合。这可以使用HQL或Criteria API来完成吗? – 2010-11-10 02:40:32