Close Menu
  • Home
  • Java
  • JavaScript
  • Hibernate
  • Deployment
  • Spring
Facebook X (Twitter) Instagram
Developers GroundDevelopers Ground
Facebook X (Twitter) Instagram
  • Home
  • Java
  • JavaScript
  • Hibernate
  • Deployment
  • Spring
Developers GroundDevelopers Ground
Home Hibernate Primary Key Generation Strategies
Hibernate

Hibernate Primary Key Generation Strategies

Akshay KulkarniBy Akshay KulkarniMarch 26, 2023Updated:August 11, 2023No Comments2 Mins Read
Facebook Twitter Pinterest LinkedIn Tumblr Reddit WhatsApp Email
Share
Facebook Twitter LinkedIn Pinterest WhatsApp Email

What is Generation Strategy?

  1. Hibernate framework can use different strategies to generate primary key values.
  2. JPA provides four strategies that generate primary keys programmatically or use database features.
  3. We can use @GenratedValue annotation to represent primary key attributes.
  4. The following are the primary key generation strategies.
    • GenerationType.AUTO
    • GenerationType.IDENTITY
    • GenerationType.SEQUENCE
    • GenerationType.TABLE

GenerationType.AUTO

    1. The GenerationType.AUTO is the default generation type and lets the persistence provider choose the generation strategy.
    2. If Hibernate is the persistence provider then it selects Generation Strategy based on a specific database vendor.
    3. Most of the database vendors use GenerationType.SEQUENCE.

GenerationType.IDENTITY

    1. This is the easiest generation strategy to generate primary keys.
    2. But this strategy completely depends on the AUTO_INCREMENTED column of the database table.
    3. It means the database vendor which supports AUTO_INCREMENT features can use this generation strategy.

GenerationType.SEQUENCE

    1. This is the most commonly used generation strategy for most database vendors.
    2. This strategy requires an extra select statement to fetch the value from the database sequence.
    3. This strategy is independent of an auto-incremented column of the database table.
    4. It means the primary key will be generated even if the database vendor does not provide an auto-increment feature.

GenerationType.TABLE

    1. The GenerationType.TABLE generates the sequence by storing and updating its current value in the database table.
    2. It keeps a separate table with primary key values.
    3. This generation strategy requires the use of pessimistic locks. In such case, the lock statement is executed to ensure that the same sequence value is not allocated for two concurrent transactions.
    4. Since this strategy uses pessimistic locks, It slows down the application performance.
Total
0
Shares
Share 0
Tweet 0
Pin it 0
Share 0
Akshay Kulkarni

Related Posts

Hibernate Configuration File

August 20, 2023

What is Hibernate? Explain important features of Hibernate

March 20, 2023
Leave A Reply Cancel Reply

Featured Posts
  • 1
    Understanding the Java String Class
    • April 10, 2025
  • 2
    Array in Java
    • February 19, 2024
  • 3
    Control Statements
    • January 16, 2024
  • 4
    Java Objects
    • January 11, 2024
  • 5
    Java Constructors
    • January 2, 2024
Facebook X (Twitter) Instagram Pinterest
© 2025 Developers Ground, All rights reserved.

Type above and press Enter to search. Press Esc to cancel.