NHibernate 3.2 LINQ n + 1求解

时间:2011-12-23 13:15:17

标签: .net linq nhibernate

我将NHibernate 3.2与LINQ一起使用,并且在SQL中有n + 1个选择查询。在映射中,获取设置为“join”。当我使用条件时,只有一个选择查询。此外,我找不到我在不同示例中看到的方法Fetch()FetchMany()。有没有办法解决使用Linq到Nhibernate 3.2的n + 1问题? 我的代码的简化版本

class News
{
    public virtual int Id { get; protected set; }
    public virtual DateTime Date { get; set; }
    public virtual Category Category { get; set; }
}

class Category
{
    public virtual int Id { get; protected set; }
    public virtual ISet<News> News { get; set; }
}

映射:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="News" namespace="NewsManagement.Models">
<class name="News" table="News">
<id name="Id">
        <generator class="native" />
</id>
<property name="Date" not-null="true" />
<many-to-one name="Category" fetch="join" column="CategoryId" class="Category, NHibernateManyToOne" not-null="true"/>
</class>
</hibernate-mapping>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="News" namespace="NewsManagement.Models">
<class name="Category" table="Categories">
    <id name="Id" column="Id">
        <generator class="native" />
    </id>
    <set name="News" fetch="join" cascade="all-delete-orphan">
        <key column="CategoryId" />
        <one-to-many class="News, NHibernateOneToMany" /> 
    </set>
</class>
</hibernate-mapping>

导致许多选择的查询:

var news = newsRepository.Linq().Skip(DefaultPageSize*currentPageIndex).Take(DefaultPageSize).OrderByDescending(x => x.Date).ToList();

这就是没有多余选择的方式:

var criteria = Session.CreateCriteria<News>();
criteria = criteria.AddOrder(Order.Desc(property));
criteria = criteria.SetMaxResults(pageSize).SetFirstResult((pageNumber - 1)*pageSize);
var news = criteria.List<News>();

2 个答案:

答案 0 :(得分:6)

试试这个:

criteria.SetFetchMode("Category",FetchMode.Eager);

或与Linq:

session.Query<News>()
                .Fetch(k => k.Category)
                .Skip(n)
                .Take(m)

或者使用QueryOver:

var result = session.QueryOver<News>
                    .Fetch(x => x.Category).Eager
                    .List();

答案 1 :(得分:1)

var result = session.Query<News>()
                    .Fetch(x => x.Category)
                    .ToList();

^这就是你要追求的吗?

.Query位于using NHibernate.Linq;

或者有QueryOver<T>

看起来像是:

var result = session.QueryOver<News>
                    .Fetch(x => x.Category).Eager
                    .List();