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

  • Home
  • SEARCH
  • 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 4332310
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T10:14:18+00:00 2026-05-21T10:14:18+00:00

Is it possible to use a multiquery and have two hql queries returning two

  • 0

Is it possible to use a multiquery and have two hql queries returning two different sets of entities where one of the sets are used in the other and that the session “fixes” this via the first level cache?

E.g. scenario (a dumb one and it could be solved with joins)

public class Room
{
  ...
  public virtual ISet<Bookings> Bookings {get;set;}
  public virtual bool IsAvailible {get;set;}
  ...
}

public class Booking
{
  ...
}

After executing a multicriteria with two hql’s:

  1. returning all rooms where
    IsAvailible = true
  2. returning all bookings having a room that has a room that IsAvailible

when accessing a room from the result and its bookings I want them to be resolved from the second resultset via the firstlevel cache of the session and there by avoiding n+1.

  • 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-21T10:14:19+00:00Added an answer on May 21, 2026 at 10:14 am

    Generally speaking, NHibernate can use the cache to “combine” the results from queries executed through Multiquery. However, it should be noted that this usually only applies to cases where lazy collections are loaded with no restrictions whatsoever.

    Examples:

    Invoice iAlias = null;
    InvoiceDetails idAlias = null;
    
    // Base-Query: get Invoices with certain condition
    var invoices = session.QueryOver<Invoice>()
        .Where(i => i.Number == "001")
        .Future<Invoice>();
    
    // Option 1: this will still cause N+1 if we iterate through invoices,
    // because it doesn't know better
    var invoicedetails = session.QueryOver<InvoiceDetails>()
        .JoinAlias(a => a.Invoice, () => iAlias)
        .Where(() => iAlias.Number == "001")
        .Future<InvoiceDetails>();
    
    // Option 2: this will still cause N+1 if we iterate through invoices,
    // because we limited the possible results using a where-condition
    var invoices2 = session.QueryOver<Invoice>()
        .Left.JoinAlias(i => i.Details, () => idAlias)
        .Where(i => i.Number == "001")
        .And(() => idAlias.Quantity > 5)
        .Future<Invoice>();
    
    // Option 3: this will work without N+1, because we don't use a filter
    // -> NHibernate will use the collection in cache
    var invoices3 = session.QueryOver<Invoice>()
        .Left.JoinAlias(i => i.Details, () => idAlias)
        .Where(i => i.Number == "001")
        .Future<Invoice>();
    
    foreach (Invoice i in invoices)
    {
        int count = i.Details.Count;
    }
    

    If we comment out two of the three options and execute the code, we will see that only option 3 will prevent a N+1, the other two will still load the InvoiceDetails for each Invoice in the loop.

    Of course this is a very simple example and it is obvious that Option 3 could also be executed without the Base-query and still return the same result, but I hope you get the idea.

    In the case where we load two different sets of entities, i.e. the root class is different as in Option 1, this “combining” will most likely not work.

    Sorry, if I used QueryOver instead of HQL, but the same rules apply.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have recently started looking into Google Charts API for possible use within the
is it possible to use captureScreenShotOnFailure for IE? maybe some kind of hacking that
Possible Duplicate: Use SVN Revision to label build in CCNET I'm working through the
Is it possible to use a flash document embedded in HTML as a link?
Is it possible to use Apache Subversion (SVN) as general purpose backup tool? (As
Is it possible to use Microsoft Entity Framework with Oracle database?
Is it possible to use an UnhandledException Handler in a Windows Service? Normally I
Is it possible to use gcov for coverage testing of multi-threaded applications? I've set
Is it possible to use an IF clause within a WHERE clause in MS
Is it possible to use both JScript and VBScript in the same HTA? Can

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.