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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:23:04+00:00 2026-05-13T18:23:04+00:00

I am using Hibernate with Spring and DB2. I am using sequences to generate

  • 0

I am using Hibernate with Spring and DB2. I am using sequences to generate primary key for entities. All entities use the same sequence HIBERNATE_SEQUENCE, which is the hibernate default.

The problem is that values that end up into primary keys are about 10 times higher than those returned by the HIBERNATE_SEQUENCE.

For example this situation just after a new row is inserted to tbl:

select max(id) as primary_key, nextval for hibernate_sequence sequence_value from tbl ;

primary_key sequence_value
501483661   50148373

I have mapped primary key like this, in super class for all entities:

@MappedSuperclass
public class AbstractEntity implements Serializable {
   @Id
   @GeneratedValue(strategy = GenerationType.SEQUENCE)
   private Integer id;

I’d like that hibernate uses those values it fetches from the sequence, not sequence values multiplied by 10. What is the correct way to do this?

  • 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-13T18:23:04+00:00Added an answer on May 13, 2026 at 6:23 pm

    Hibernate seems to do following:

    When hibernate needs a primary key it will fetch the value from sequence. Hibernate will generate several primary key values from single sequence values. For example hibernate will keep a internal counter whose value is appended to sequence value to obtain the primary key value. When the internal counter hits its limit the counter is reset, a new value from sequence is obtained and primary key process starts all over again.

    For example:

    1. Value obtained from sequece is 123. The obtained sequence value is stored per session.
    2. For current session the generated primary keys are 1230, 1231, 1232, 1233, …, 1238, 1239. A counter value is concatenated to sequence value obtained in step 1. A key is generated when needed.
    3. Now primary key generation process start all over. Goto 1.

    This causes following effects:

    • database sequence value is effectively multiplied by 10
    • Hibernate does not have to make a database read for every DB insert it makes. The above algorithm cuts the number of sequence reads down to 10% (when doing a lot of inserts in single session).
    • every other non-hibernate application must use similar algorithm to generate primary keys from sequence, otherwise there will be primary key conflicts at some point

    To make hibernate use actual values obtained from sequence, this mapping can be used:

    @Id
    @GeneratedValue(generator="hibernate_sequence")
    @GenericGenerator(strategy="sequence", name="hibernate_sequence")
    private Integer id;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Hibernate Validator 4.0.2, Spring 3.0 and Hibernate 3.3.2 (which, as I understand
I'm working on a Spring MVC project in which I'm using Hibernate Validator to
I'm using Hibernate and Spring and I'm currently stuck with something which I thought
greetings all i have a web application using (spring-hibernate) frameworks and when tried to
I have the same issue using hibernate against DB2 and MySQL. Here is a
I'm using Hibernate and Spring with the DAO pattern (all Hibernate dependencies in a
Still no luck with C3P0 pooling in Hibernate using Spring WebFlow if I use
i have spring project under java, using hibernate query, i like to use pessimistic
I have an application which are using Hibernate / Spring and Spring MVC, but
As best practice, when using hibernate and spring together which transaction management should I

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.