Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7496791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T18:47:18+00:00 2026-05-29T18:47:18+00:00

I need to efficiently load a large number of entities based on a list

  • 0

I need to efficiently load a large number of entities based on a list of ad hoc IDs. Unfortunately it doesn’t look like the 2nd level cache is checked first when making Restrictions.In(Projections.Id(), ids) criteria queries.

I guess I’m looking for something similar to ISession.Load that takes a collection of IDs (instead of just one) and only execute an IN query for entities that can’t be found in either the 1st or 2nd level caches. If nothing like this exists then what’s the easiest way to check the two caches manually without resorting to reflection?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T18:47:19+00:00Added an answer on May 29, 2026 at 6:47 pm

    Darren Kopp‘s comment is absolutely right, batching automatically handles this provided you remember to use ISession.Load instead of ISession.Get and you don’t touch any of the proxy objects until you’ve finished fetching them all.

    However I thought I’d share another novel solution I came up with for environments that don’t have lazy loading or batching enabled. By subclassing the DefaultLoadEventListener class and returning null from the LoadFromDatasource method it’s possible to load an entity if it exists in the 1st or 2nd level caches without touching the database. It’s then a simple matter of fetching the missing entities in a single IN query and weaving them into an ordered collection of results.

    static IList<T> LoadAll<T>(this ISession session, params object[] ids)
        where T : class
    {
        var results = new T[ids.Length];
        var uncachedIds = new Dictionary<object, int>();
        var helper = new LoadHelper();
        for (var i = 0; i < ids.Length; i++) {
            var id = ids[i];
            var evt = new LoadEvent(id, typeof (T).FullName, false,
                                    (SessionImpl) session);
            helper.OnLoad(evt, LoadEventListener.Get);
            var entity = (T) evt.Result;
            if (entity != null) {
                results[i] = entity;
            }
            else {
                uncachedIds.Add(id, i);
            }
        }
        if (uncachedIds.Count > 0) {
            var entities = session.CreateCriteria<T>()
                .Add(Restrictions.In(Projections.Id(), uncachedIds.Keys))
                .List<T>();
            foreach (var entity in entities) {
                var id = session.GetIdentifier(entity);
                var index = uncachedIds[id];
                results[index] = entity;
            }
        }
        return results;
    }
    

    Here’s the very simple LoadHelper class that does the heavy lifting:

    private class LoadHelper : DefaultLoadEventListener
    {
        protected override object LoadFromDatasource(LoadEvent evt,
                                                     IEntityPersister persister,
                                                     EntityKey keyToLoad,
                                                     LoadType options)
        {
            return null;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a need to run a relatively large number of virtual machines on
Background: I have a large 2D array of integers that I need to load
In Python I need to efficiently and generically test whether an attribute of a
I need to multiply several 1000s digits long integers as efficiently as possible in
I am having trouble efficiently selecting the information I need to display. Hopefully someone
I need to randomly 'sort' a list of integers (0-1999) in the most efficient
I'm trying to determine how to find/retrieve/load objects efficiently in terms of a.) minimizing
I need to delete a range of nodes, up to a certain number, in
I have a list of files, which need to be read, in chunks, into
I need to process a large file, around 400K lines and 200 M. But

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.