Quick Contact

    Hibernate

    Hibernate/JPA – Primary Key Generation

    The JPA specification supports 4 different primary key generation strategies that generate the primary key values programmatically or use database features, like auto-incremented columns or sequences.

    4 options to generate primary keys

    The JPA specification supports 4 different primary key generation strategies that generate the primary key values programmatically or use database features, like auto-incremented columns or sequences. The only thing you have to do is to add the @GeneratedValue annotation to your primary key attribute and choose a generation strategy.

    @Id
    @GeneratedValue
    @Column(name="id", updatable=false, nullable=false)
    privateLong id;
    
    1. GenerationType.AUTO

      The GenerationType.AUTO is the default generation type and lets the persistence provider choose the generation strategy.

      For example, consider we have a Student JPA entity class with GenerationType.AUTO as generation type:

      @Entity
      @Table(name="student")
      publicclassStudent {
      
      @Id
      @GeneratedValue(strategy=GenerationType.AUTO)
      @Column(name="id", updatable=false, nullable=false)
      privateLong id;
      
      @Column(name="first_name")
      privateStringfirstName;
      
      @Column(name="last_name")
      privateStringlastName;
      
      @Column(name="email")
      privateString email;
      
      publicStudent() {
      
          }
      
      // getter and setters
      }
      

      If you use Hibernate as your persistence provider, it selects a generation strategy based on the database-specific dialect. For most popular databases, it selects GenerationType.SEQUENCE which I will explain in a further section.

    2. GenerationType.IDENTITY

      The GenerationType.IDENTITY is the easiest to use but not the best one from a performance point of view. It relies on an auto-incremented database column and lets the database generate a new value with each insert operation. From a database point of view, this is very efficient because the auto-increment columns are highly optimized, and it doesn’t require any additional statements.

      For example, consider we have a Student JPA entity class with GenerationType.IDENTITY as a generation type:

      @Entity
      @Table(name="student")
      publicclassStudent {
      
      @Id
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      @Column(name="id", updatable=false, nullable=false)
      privateLong id;
      
      @Column(name="first_name")
      privateStringfirstName;
      
      @Column(name="last_name")
      privateStringlastName;
      
      @Column(name="email")
      privateString email;
      
      publicStudent() {
      
          }
      
      // getter and setters
      }
      
    3. GenerationType.SEQUENCE

      The GenerationType.SEQUENCE is to generate primary key values and uses a database sequence to generate unique values.

      It requires additional select statements to get the next value from a database sequence. But this has no performance impact on most applications. And if your application has to persist a huge number of new entities, you can use some Hibernate specific optimizations to reduce the number of statements.

      For example, consider we have a Student JPA entity class with GenerationType.SEQUENCE as generation type:

      @Entity
      @Table(name="student")
      publicclassStudent {
      
      @Id
      @GeneratedValue(strategy=GenerationType.SEQUENCE)
      @Column(name="id", updatable=false, nullable=false)
      privateLong id;
      
      @Column(name="first_name")
      privateStringfirstName;
      
      @Column(name="last_name")
      privateStringlastName;
      
      @Column(name="email")
      privateString email;
      
      publicStudent() {
      
          }
      
      // getter and setters
      }
      

      If you don’t provide any additional information, Hibernate will request the next value from its default sequence. You can change that by referencing the name of a @SequenceGenerator in the generator attribute of the @GeneratedValue annotation.

      The @SequenceGenerator annotation lets you define the name of the generator, the name, and schema of the database sequence and the allocation size of the sequence.

      @Id
      @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="book_generator")
      @SequenceGenerator(name="book_generator", sequenceName="book_seq", allocationSize=50)
      @Column(name="id", updatable=false, nullable=false)
      privateLong id;
      
    4. GenerationType.TABLE

      The GenerationType.TABLE gets only rarely used nowadays. It simulates a sequence by storing and updating its current value in a database table which requires the use of pessimistic locks that put all transactions into sequential order. This slows down your application, and you should, therefore, prefer the GenerationType.SEQUENCE, if your database supports sequences, which most popular databases do.

      For example, consider we have a Student JPA entity class with GenerationType.TABLE as generation type:

      @Entity
      @Table(name="student")
      publicclassStudent {
      
      @Id
      @GeneratedValue(strategy=GenerationType.TABLE)
      @Column(name="id", updatable=false, nullable=false)
      privateLong id;
      
      @Column(name="first_name")
      privateStringfirstName;
      
      @Column(name="last_name")
      privateStringlastName;
      
      @Column(name="email")
      privateString email;
      
      publicStudent() {
      
          }
      
      // getter and setters
      }
      

      You can use the @TableGenerator annotation in a similar way as the already explained @SequenceGenerator annotation to specify the database table which Hibernate shall use to simulate the sequence.

      @Id
      @GeneratedValue(strategy=GenerationType.TABLE, generator="book_generator")
      @TableGenerator(name="book_generator", table="id_generator", schema="bookstore")
      @Column(name="id", updatable=false, nullable=false)
      privateLong id;
      


    Apply now for Advanced Java Training Course

    Copyright 1999- Ducat Creative, All rights reserved.

    Anda bisa mendapatkan server slot online resmi dan terpercaya tentu saja di sini. Sebagai salah satu provider yang menyediakan banyak pilihan permainan.