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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:46:24+00:00 2026-05-26T05:46:24+00:00

I have 3 classes that are mapped via NHibernate: Intersection, Vehicle and Zone. My

  • 0

I have 3 classes that are mapped via NHibernate: Intersection, Vehicle and Zone.

My Intersection class contains a list of Zones that belong to the Intersection. My Vehicle class contains a list of Zones that the Vehicle contains. Finally my Zone class contains a list of Vehicles that contain the Zone.

My test performance set consists of 10,000 Vehicles and 500 Zones and 250 Intersections. My load time with the Zones and Vehicles both having the lists mapped is right around 27 minutes for all these objects.

I am not sure what is going on but these two lists are not optimized in the least. There is a 10 minute difference in saving the objects if I take the Vehicle list out of the Zone class mapping. This seems a bit off seeing as how the 2 lists are directly related to each other.

It appears NHibernate is recursively saving the items within both the lists and adding a bunch of overhead to the saving procedure. Is there any way to optimize these lists for faster time saving the objects?

Here are my mappings for Device which both my Intersection and Vehicle inherit from:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class xmlns="urn:nhibernate-mapping-2.2" name="EMTRAC.Devices.Device, EMTRAC_v3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`Device`">
    <id name="PK" type="System.Int64, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="PK" />
      <generator class="identity" />
    </id>
    <version name="LastModifiedOn" column="LastModifiedOn" type="timestamp" access="field.pascalcase-underscore" />
    <joined-subclass name="EMTRAC.Intersections.Intersection, EMTRAC_v3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
      <key>
        <column name="Device_id" />
      </key>      
      <component name="Zones" access="property">
        <bag name="_list" cascade="all-delete-orphan" access="field" fetch="join" inverse="false">
          <key>
            <column name="Zone_PK" />
          </key> 
          <many-to-many class="EMTRAC.Zones.Zone, EMTRAC_v3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
        </bag>
      </component>
      <property name="ID" type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
        <column name="ID" />
      </property>
    </joined-subclass>
    <joined-subclass name="EMTRAC.Vehicles.Vehicle, EMTRAC_v3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
      <key>
        <column name="Device_id" />
      </key>      
      <component name="Zones" access="property">
        <bag name="_list" cascade="save-update" access="field" table="VehicleZones" inverse="true">
          <key>
            <column name="veh_id" not-null="true"/>
          </key>
          <many-to-many class="EMTRAC.Zones.Zone, EMTRAC_v3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
        </bag>
      </component>
      <property name="ID" type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
        <column name="ID" />
      </property>           
    </joined-subclass>
  </class>
</hibernate-mapping>    

And finally here is my mapping for the Zone class:

    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class xmlns="urn:nhibernate-mapping-2.2" name="EMTRAC.Zones.Zone, EMTRAC_v3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`Zone`">
    <id name="ID" type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="PK"/>
      <generator class="identity" />
    </id>
    <version name="LastModifiedOn" column="LastModifiedOn" type="timestamp" access="field.pascalcase-underscore" />
    <property name="ID" type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="ID" />
    </property>    
    <component name="Vehicles" access="property">
      <bag name="_list" cascade="save-update" access="field" table="VehicleZones">
        <key>
          <column name="veh_id" not-null="true"/>
        </key>
        <many-to-many class="EMTRAC.Vehicles.Vehicle, EMTRAC_v3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
      </bag>
    </component>
  </class>
</hibernate-mapping>

Any ideas how to improve the efficiency of these lists? Right now each table it is saving them in contains 500,000 records and of course loading them and saving that many records is taking some time.

Edit

I removed all the lazy=”false” parts I had forgotten to remove and it is substantially faster. I also made a few adjustments to the saving and loading of the objects, breaking the parts into separate threads to increase performance and implementing the use of a transaction in NHibernate appeared to help.

However I have ran into one snag. I am now unable to save both lists. I have pre-saved all my intersections, vehicles, and zones before adding the lists to the Vehicles and the Zones. However, if I include the list of Vehicles in the Zone mapping I obtain an error trying to update the Vehicles and the lists. Here is the code:

using (var tx = session.BeginTransaction())
{
    foreach (Vehicle vehicle in Program.data.Vehicles.list)
    {
        session.Update(vehicle);
    }
    tx.Commit();
}

I receive a Stack Overflow exception. Any ideas?

Any idea how to get this to go through?

Edit
Is there no way to map the list of Vehicles in the Zone class and the list of Zones in the Vehicle class to the same table so it is not iterating through both lists recursively? The lists are related to each other. One is a list of Zones that a Vehicle belongs to and the other is a list of Vehicles that a Zone belongs to. I don’t think I have that mapped correctly after digging more into this.

EDIT
I have been making progress with the correct mappings I believe. I posted a more simplified version of the mappings for Device which maps the Intersection and Vehicle classes along with a simplified version of Zone which of course maps the Zone class.

This appears to save relatively well with one exception. Saving large numbers of items is tossing out a Stack Overflow exception for some reason. Here is the approach I am using:

//  Create a Transaction for batch updating
using (var tx = session.BeginTransaction())
{
    foreach (Vehicle veh in Program.data.Vehicles.list)
    {
        session.Save(veh);
    }
    //  Commit transactions
    tx.Commit();
}

This works fine for say 250 Intersections, 500 zones, and 1000 Vehicles. However, I was attempting to create a test set of data to stress this in a more extreme environment and bumped up to 2400 Intersections, 9600 Zones, and 5000 Vehicles and I run across a Stack Overflow exception when trying to save the test data into the database via this manner.

Any ideas?

  • 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-26T05:46:24+00:00Added an answer on May 26, 2026 at 5:46 am

    It was actually recursively saving them b/c I had the mapping incorrect. I needed an inverse relation on one side and then I needed to modify the mapping to point to the foreign key as well. Finally I needed a Modified field there to tell NHibernate that the item had been modified and that it needed updating.

    Here is the correct mapping:

    Vehicle:

    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
      <class xmlns="urn:nhibernate-mapping-2.2" name="EMTRAC.Devices.Device, EMTRAC_v3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`Device`">
        <id name="PK" type="System.Int64, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
          <column name="PK" />
          <generator class="identity" />
        </id>
        <version name="LastModifiedOn" column="LastModifiedOn" type="timestamp" access="field.pascalcase-underscore" />
        <joined-subclass name="EMTRAC.Vehicles.Vehicle, EMTRAC_v3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
          <key>
            <column name="Device_id" />
          </key>      
          <component name="Zones" access="property">
            <bag name="_list" cascade="save-update" access="field" table="VehicleZones" inverse="true">
              <key>
                <column name="veh_id" not-null="true"/>
              </key>
              <many-to-many class="EMTRAC.Zones.Zone, EMTRAC_v3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
            </bag>
          </component>
          <property name="ID" type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
            <column name="ID" />
          </property>     
      </class>
    </hibernate-mapping>
    

    Zone:

    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
      <class xmlns="urn:nhibernate-mapping-2.2" name="EMTRAC.Zones.Zone, EMTRAC_v3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`Zone`">
        <id name="ID" type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
          <column name="PK"/>
          <generator class="identity" />
        </id>
        <version name="LastModifiedOn" column="LastModifiedOn" type="timestamp" access="field.pascalcase-underscore" />
        <property name="ID" type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
          <column name="ID" />
        </property>    
        <component name="Vehicles" access="property">
          <bag name="_list" cascade="save-update" access="field" table="VehicleZones">
            <key>
              <column name="veh_id" not-null="true"/>
            </key>
            <many-to-many class="EMTRAC.Vehicles.Vehicle, EMTRAC_v3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
          </bag>
        </component>
      </class>
    </hibernate-mapping>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that contains a list of objects. I previously had this
I have the following mapped classes: <class name=Company table=Company> <id name=ID column=CompanyID> <generator class=native/>
I have a number of classes that are mapped to tables with SQLAlchemy (non-declaratively
I have a 2 classes that share a UUID and are uni-directionally mapped. I
I have two classes mapped using fluent NHibernate - User and a UserRoleAssignment. An
I have three classes mapped using the table-per-subclass class mapping strategy. The tables are:
I have a mapped class that has a date property. I would like to
I have classes that are named exactly the same across different plug-ins that I
I have classes that are needed in both my web service and my server.
I have three classes that I am using and have shortened for ease of

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.