简体   繁体   中英

Bounding an eagerly fetched collection

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("Pictures", 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM