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 3783156
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T11:11:04+00:00 2026-05-19T11:11:04+00:00

I just started playing with NHibernate Linq and found a strange behavior. I have

  • 0

I just started playing with NHibernate Linq and found a strange behavior. I have a class Category and a class Product. Category has a list of Products with one to many association. Here’s mappings:

  <class name="Category">
    <id name="Id">
      <generator class="hilo" />
    </id>
    <property name="Name" lazy="false" length="20" />

    <bag name="Products" cascade="none" lazy="false" inverse="true" fetch="join">
      <key column="CategoryId" />
      <one-to-many class="Product" />
      <!--<filter name="IdFilter" />-->
    </bag>
  </class>

  <class name="Product">
    <id name="Id">
      <generator class="hilo" />
    </id>
    <property name="Name" lazy="false" />
    <property name="Discontinued" lazy="false" />
    <property name="Price" lazy="false" />
    <many-to-one name="Category"
             class="Category"
             column="CategoryId"
             cascade="none" />
  </class>

When I query categories with this query

var cs = session.Query<Category>().Where(c => c.Products.Any(p => p.Price == 13.3392)).ToList();

I look at NHibernate profiler and see this results

select category0_.Id   as Id1_,
       category0_.Name as Name1_
from   Category category0_
where  exists (select products1_.Id
               from   Product products1_
               where  category0_.Id = products1_.CategoryId
                      and products1_.Price = 13.3392 /* @p0 */)

SELECT products0_.CategoryId   as CategoryId1_,
       products0_.Id           as Id1_,
       products0_.Id           as Id0_0_,
       products0_.Name         as Name0_0_,
       products0_.Discontinued as Disconti3_0_0_,
       products0_.Price        as Price0_0_,
       products0_.CategoryId   as CategoryId0_0_
FROM   Product products0_
WHERE  products0_.CategoryId = 131073 /* @p0 */

SELECT products0_.CategoryId   as CategoryId1_,
       products0_.Id           as Id1_,
       products0_.Id           as Id0_0_,
       products0_.Name         as Name0_0_,
       products0_.Discontinued as Disconti3_0_0_,
       products0_.Price        as Price0_0_,
       products0_.CategoryId   as CategoryId0_0_
FROM   Product products0_
WHERE  products0_.CategoryId = 32768 /* @p0 */

The category is fetched one by one from database. So the amount of roundtrips to database equals to the amount of category objects matched by where clause.

Is there a way to tell NHibernate to optimize the query? I’m completely lost.

Thank you very much for your support.

  • 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-19T11:11:05+00:00Added an answer on May 19, 2026 at 11:11 am

    on your bag mapping class try adding batch-size

    <bag name="Products" batch-size="25" ...>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have just started playing with ASP.NET MVC and have stumbled over the following situation.
I just started playing around with the 960 CSS framework and found that it
I just started playing with Linq to entities in a windows forms application and
I have just started playing with the ASP.Net MVC framework, and today I created
I just started playing with memcache(d) last night so I have a LOT to
I just started playing with lambdas and Linq expression for self learning. I took
I´ve just started playing around with Java and Hibernate and have gotten an application
Just started playing with Android and I'm trying to create an app that has
I have just started playing with storyboards in XCode 4.3 and while designing an
I have just started playing with TeamCity and its great. Now I got it

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.