开发者

How does NHibernate seqhilo generate ids?

开发者 https://www.devze.com 2023-03-13 18:09 出处:网络
My Oracle database has a sequence (PART_SEQ) starting at 1,000,000,000.My NHibernate mapping uses this sequence for id generation using seqhilo.

My Oracle database has a sequence (PART_SEQ) starting at 1,000,000,000. My NHibernate mapping uses this sequence for id generation using seqhilo.

  <id name="_persistenceId" column="Id" type="long" access="field" unsaved-value="0" >
     <generator class="seqhilo" >
        <param name="sequence">part_seq</param>
        <param name="max_lo">100</param>
     </generator>
  </id>

I am expecting ids to be generated like 100000000000, 100000000001, 100000000002, ..., 100000000100, 100000000101, 100000000102, ... based 开发者_Go百科on this question and this question.

Instead NHibernate is producing ids like 101000000000, 101000000001, 101000000002, ..., 101000000100, 101000000101, 101000000102 with an extra 1. How is it producing these ids and how can I get it to not add the extra 1 to the id?


Looking at the source here, the implementation of the algorithm adds 1 to the max_lo value before multiplying it by the sequence to generate the hi part. The lo part is then added to this result to create the final id.

For the example in the question it will produce ids in the following manner:

//max_lo is set to 100 when the generator is initialized

if(lo > max_lo)
{
    lo = 1;
    sequence = GetNextSequenceNumber(...);  //Starts at 1 billion
    hi = sequence * (max_lo + 1);
}
returnVal = hi + lo++;

Creating 101000000001, 101000000002, 101000000003, etc.

0

精彩评论

暂无评论...
验证码 换一张
取 消