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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:12:34+00:00 2026-06-03T04:12:34+00:00

I have a class structure like so: public class Person { public virtual int

  • 0

I have a class structure like so:

public class Person
{
    public virtual int Id { get; protected internal set; }
    public virtual string Name { get; set; }
}

public class Customer : Person
{
    public virtual string AccountNumber { get; set; }
}

Customer is mapped as a subclass of Person (using FluentNHibernate’s SubclassMap<T>), and the table structure is table-per-subclass (separate Person and Customer tables sharing an Id column).

In my test, I open a stateless session and try to insert a series of Person entities:

using (var stateless = sessionFactory.OpenStatelessSession())
using (var transaction = stateless.BeginTransaction())
{
    var persons = new[]
    {
        new Person { Name = "Alice" },
        new Person { Name = "Bob" },
        new Customer { Name = "Cathy", AccountNumber = "001" },
        new Customer { Name = "Dave", AccountNumber = "002" }
    };
    foreach (var person in persons)
        stateless.Insert(person);
    transaction.Commit();
}

If I run this with the ShowSql switch on, I can see that there are no INSERT statements generated on the Person table (meaning that they are batched), but there are individual INSERT statements being generated for the Customer table (from which I infer that these are not being batched).

Oddly enough, I’ve found that if the derived type (i.e. Customer) has its own collection (let’s call them Orders) and I Insert each item in that collection directly into the same stateless session (not into the actual collection), it batches those too and has no trouble resolving the relationships. This behaviour seems to be entirely limited to derived classes of polymorphic entities.

Is this expected behaviour? If so, is there any way that I can rewrite the insertion code above to ensure that all of the subclass tables are batched as well?

(Note: I am using the SequenceHiLoGenerator for all IDs, and I’ve configured the AdoNetBatchSize accordingly, so this is not a general issue with batching, as far as I can tell. I can see the HiLo table being hit when a batch operation would be occurring.)

  • 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-06-03T04:12:35+00:00Added an answer on June 3, 2026 at 4:12 am

    After several days of digging and experimentation, the answer seems to be “yes”. There are various scattered blog posts mentioning in passing that NHibernate has trouble batching subclasses – specifically when using the table per subclass strategy (which I’ve been using).

    It’s not an issue with the table-per-class-hierarchy strategy. Unfortunately, it was way too late in the game for me to be changing the persistence model (plus, I really dislike TPH from a relational DB design perspective), so I had to look at other options.

    I was able to work around the issue in the following way for inserts:

    1. Make the base class concrete (it used to be abstract);
    2. Make a shallow copy of each object to be inserted (constructing only the base class);
    3. Insert the copies as usual;
    4. Flush the session to meet referential integrity constraints;
    5. Use SqlBulkCopy to insert the derived classes into their own tables, using some scary voodoo based on casting the IClassMetadata to AbstractEntityPersister in order to get all of the column/table mappings;
    6. Insert any mapped collections owned by subclasses using the IStatelessSession.Insert method as normal (repeat this process for collections with their own class hierarchies).

    Updates, I imagine, would be a lot more complicated, and probably very difficult to handle without relying on hard-coded table/column names; if I had to do this, I would probably rely on Stored Procedures (with Table-Valued Parameters, or staging tables) instead.

    The resulting code is not pleasant to look at, but it makes for a significant performance improvement – at least 25% for normal-sized batches of about 20-30k.

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

Sidebar

Related Questions

I have the following data structure: public class Person { public string Name{get;set;} public
Say I have a structure like: class SomeObject Public Name as String Public Created
i have a data structure like this public class Employee { public string Name
I have a class structure that looks like this: class Person { public virtual
I have a object structure like this: public class Entity { IList<Relationship> Relationships{get;set;} }
I have a structure like below: public class BaseClass { public string SendError(string Message){
I have a class structure like this public class Entity { public int Id
I have a Product class something like: class Product { public string Name; public
I have a class structure like abstract class Animal { public Animal(){ //init stuff..
I have a class structure like: abstract class A { String a; } class

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.