开发者

Bounding an eagerly fetched collection

开发者 https://www.devze.com 2023-01-11 21:20 出处:网络
How can I eagerly fetch a collection, but just the N first items? Using this code works, but is there an \'official way\' to achieve that?

How can I eagerly fetch a collection, but just the N first items?

Using this code works, but is there an 'official way' to achieve that?

public Gallery GetById(int id)
{
    var session = GetSession();

    var criteria = session.CreateCriteria<Gallery>()
        .Add(Expression.Eq("Id", id))

        .SetFetchMode("Pic开发者_开发百科tures", FetchMode.Eager)         
        .CreateAlias("Pictures", "p")

        .SetFirstResult(0)
        .SetMaxResults(24)
        ;

    return criteria.UniqueResult<Gallery>();
}

In this case, I'm bounding the results of Gallery, which is anyway unique result, but I want to bound the results of Pictures.


Your code works correctly, and is perfectly acceptable. If you want to always eagerly fetch, you can set it as such in your table mapping configuration (HBM, Fluent, or with whatever solution you use), and then explicitly tell it not to for the cases where you don't want to eagerly fetch. Both ways work fine and are acceptable. Use whichever is more convenient or safe for your project needs and team's coding style.

0

精彩评论

暂无评论...
验证码 换一张
取 消