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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:31:46+00:00 2026-06-12T01:31:46+00:00

I need to do bulk inserts into SQLite database with NHibernate. The object’s PK

  • 0

I need to do bulk inserts into SQLite database with NHibernate. The object’s PK is HiLo int32. I tried to use NH stateless session with session.insert, and it works fine. However, I found that using prepared command is faster about 30-40%, so I am trying to utilize it.

Currently I am struggling with assigning id (which is NH HiLo).
I found a solution on stackoverflow (NHibernate HiLo ID Generator. Generating an ID before saving) – see method GenerateIdentifier(), but it queries and updates the database on each call, so it’s not a good option.

It there any way to make it working as it supposed to – when id generator reaches the hivalue, it shold do only 1 roundtrip to the server to get the new low and hi values ?

public static Int64 TestNHSQLiteBulk(bool newDB)
        {
            int ProjectCount = 1000000;

            var factory = Database.CreateSQLiteSessionFactory(newDB);

            Int64 objectCount = 0;

            using (var session = factory.OpenStatelessSession())
            {
                var connection = session.Connection;
                using (var transaction = session.BeginTransaction(System.Data.IsolationLevel.ReadCommitted))
                {

                    // this should be removed when proper HiLo will be implemented.
                    var nextProject = session.CreateCriteria<Project>().SetProjection(Projections.Max<Project>(p => p.ProjectId)).UniqueResult();
                    int startId = (nextProject == null) ? 0 : (int)nextProject + 1;

                    var command = connection.CreateCommand();
                    command.CommandText = "Insert INTO Project (ProjectId, ProjectName) Values(?,?)";

                    var projectIdParameter = command.CreateParameter();
                    projectIdParameter.ParameterName = "ProjectId";
                    projectIdParameter.DbType = System.Data.DbType.Int32;

                    var projectNameParameter = command.CreateParameter();
                    projectNameParameter.ParameterName = "ProjectName";
                    projectNameParameter.DbType = System.Data.DbType.String;

                    command.Parameters.Add(projectIdParameter);
                    command.Parameters.Add(projectNameParameter);
                    command.Prepare();

                    for (int p = startId; p < startId + ProjectCount; p++)
                    {

                        var project = new Project()
                        {
                            ProjectName = "Project " + p
                        };

                        //found on stackoverflow, but is results a roundtrip to server on each call.
                        GenerateIdentifier(project, Database.savedConfig, session);

                        //session.Insert(project);

                        //using prepared command is almost 2x faster!
                        projectIdParameter.Value = project.ProjectId;
                        projectNameParameter.Value = project.ProjectName;
                        command.ExecuteNonQuery();

                        objectCount++;
                    }

                    transaction.Commit();
                }

            }

            return objectCount;
        }

    //this will get the value and update the hi-lo value repository in the datastore
    public static void GenerateIdentifier(object target, NHibernate.Cfg.Configuration conf, IStatelessSession session)
    {
        var targetType = target.GetType();

        var classMapping = conf.GetClassMapping(targetType);
        var impl = session.GetSessionImplementation();

        var newId = classMapping.Identifier.CreateIdentifierGenerator(impl.Factory.Dialect, classMapping.Table.Catalog, classMapping.Table.Schema,
                                                                classMapping.RootClazz).Generate(impl, target);
        classMapping.IdentifierProperty.GetSetter(targetType).Set(target, newId);
    }
  • 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-12T01:31:47+00:00Added an answer on June 12, 2026 at 1:31 am

    you are creating a new HiLogenerator for each entity instead of using the given one. Also the generator normqally initializes itself to the value in the database so it is not nessesary to query for that manually:

    public static Int64 TestNHSQLiteBulk(bool newDB)
    {
        int ProjectCount = 1000000;
    
        var factory = Database.CreateSQLiteSessionFactory(newDB);
    
        var classMapping = (SingleTableEntityPersister)factory.GetClassMetadata(typeof(Project));
        var generator = classMapping.IdentifierGenerator;
    
        Int64 objectCount = 0;
    
        using (var session = factory.OpenStatelessSession())
        {
            var connection = session.Connection;
            using (var transaction = session.BeginTransaction(System.Data.IsolationLevel.ReadCommitted))
            {
                var command = connection.CreateCommand();
                command.CommandText = "Insert INTO Project (ProjectId, ProjectName) Values(?,?)";
    
                var projectIdParameter = command.CreateParameter();
                projectIdParameter.ParameterName = "ProjectId";
                projectIdParameter.DbType = System.Data.DbType.Int32;
    
                var projectNameParameter = command.CreateParameter();
                projectNameParameter.ParameterName = "ProjectName";
                projectNameParameter.DbType = System.Data.DbType.String;
    
                command.Parameters.Add(projectIdParameter);
                command.Parameters.Add(projectNameParameter);
                command.Prepare();
    
                for (int p = 0; p < ProjectCount; p++)
                {
                    var projectId = (long)generator.Generate(session.GetSessionImplementation(), null);
    
                    //using prepared command is almost 2x faster!
                    projectIdParameter.Value = projectId;
                    projectNameParameter.Value = "Project" + projectId;
                    command.ExecuteNonQuery();
    
                    objectCount++;
                }
    
                transaction.Commit();
            }
        }
    
        return objectCount;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need make all of my posts update. I use bulk upload for store,
I'm doing a bulk insert but before inserting into the actual table I need
I want to bulk insert about 700 records into the Android database on my
I need some help understanding how Python and postgres handle transactions and bulk inserts
I need to bulk load a large amount of data (about 7.000.000 entries) into
I need to upload files 'into' a SQL Server database. I need a solution
I need to programmatically insert tens of millions of records into a Postgres database.
A while back when I was performing some bulk inserts of data into my
I need to do bulk-insert of document in my CouchDB database. I'm trying to
I need to import the data form .csv file into the database table (MS

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.