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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:12:22+00:00 2026-05-25T06:12:22+00:00

I have one application which uses NHibernate to save entities to a database, and

  • 0

I have one application which uses NHibernate to save entities to a database, and it uses the HiLo generate of NHibernate to create the id’s. I have another application which I need to save data to the same table, however in this application, I’m not using NHibernate. Is there a simple way to use HiLo without adding reference to NHibernate and mapping files?

<id name="EntityId" unsaved-value="0" >
    <column name="EntityId" sql-type="int" not-null="true" />
    <generator class="hilo">
    <param name="table">hibernate_unique_key</param>
        <param name="column">next_hi</param>
        <param name="max_lo">1000</param>
    </generator>
</id>

Update: I created a new method to get the next id which I believe has the same behavior as the NHibernate HiLo sequence. I’m not putting any locks on the database, so if this were a high frequency table that I was inserting to, it could be an issue, but for very low frequency, there is a very low possibility that there may be a concurrency issue.

/// <summary>
/// Gets the next entity id.
/// </summary>
/// <returns></returns>
public static int GetNextAvailableEntityId()
{
    int max_lo = 1000;
    int nextHi = DefaultContext.Database.SqlQuery<int>("select next_hi from hibernate_unique_key").Single();
    int nextRangeStart = max_lo * nextHi;
    int currentMax = DefaultContext.Database.SqlQuery<int>("SELECT MAX(entityid) FROM Entities").Single();

    if (currentMax < nextRangeStart)
    {
        return currentMax + 1;
    }
    else
    {
        DefaultContext.Database.ExecuteSqlCommand("update hibernate_unique_key set next_hi = next_hi + 1");
        return nextHi;
    }
}
  • 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-25T06:12:22+00:00Added an answer on May 25, 2026 at 6:12 am

    If in doubt, take a look at NH’s code to be sure that you implement it correctly.

    It is something like this:

    • open a separate transaction, increment and read the next high value, commit
    • create 1000 ids by next_high * 1000 + x
    • when you run out of ids, start again

    You can create a single instance of an id generator for all transaction of the same process. Make sure that your id generator is thread safe.

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

Sidebar

Related Questions

We have an WCF application which uses NHibernate to query data from the database.
I have winforms application which uses nhibernate and active records to access the database.
I have one application which uses the standard .NET forms authentication cookie, now I
I have an application which obtains data in JSON format from one of our
I have two windows application, one is a windows service which create EventWaitHandle and
I have a Spring application which uses Hibernate on a PostgreSQL database. I'm trying
I have an application which uses SslStream to send and receive data with its
I have an application which uses version 8.0.50727.5592 of the CRT. One of the
I have a application which uses socket connection to send and receive data from
I have a application which uses two instances of one TableViewController in two different

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.