Site icon NgDeveloper

GenerationType.IDENTITY vs GenerationType.SEQUENCE vs GenerationType.AUTO

GenerationType.IDENTITY vs GenerationType.SEQUENCE vs GenerationType.AUTO:

In hibernate for the primary key/auto generated id value column we used to annotate @GeneratedValue with strategy as either of these,

  1. GenerationType.IDENTITY
  2. GenerationType.SEQUENCE
  3. GenerationType.AUTO

It is highly important to understand the difference between these generation types to pick the best one for use cases.

Suppose you have two tables namely store and coupon, store id is a primary key in store table and foreign key in coupon table. Because one store can have multiple coupons/offers/deals.

Store table: store_id | store_name | store_url

Coupon table: coupon_id | coupon_name | store_id

Difference between GenerationType.IDENTITY / GenerationType.SEQUENCE / GenerationType.AUTO

1. GenerationType.IDENTITY

In the above use case if you mention GenerationType.IDENTITY in store_id and coupon_id field in both store and coupon entities then the auto generated id value will be generated from value 1 in both the table and both are independent to their own table sequences.

Coupon Entity: (same way create entity for Store also)

@Entity
public class Coupon {

  public Coupon() {

  }

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "coupon_id")
  private Long couponId;

  // getter & setters for couponId

}

Note: Same way configure it for store as well.

All Tables (No extra tables created)

Store table: (sequences are starting from 1)

Coupon table: (sequences are starting from 1)

Things to Note:

2. GenerationType.SEQUENCE

Hibernate creates the sequence for our tables if we specify the column with GenerationType.SEQUENCE. We can also add our sequence to be used if needed.

We can generate the auto incremental values by configurating the separate sequences for every id auto increment field also. In this along with our entities, sequences will also be created.

Coupon Entity: (same way create entity for Store also)

@Entity
public class Coupon {

  public Coupon() {

  }

  @Id
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "coupon_seq")
  @SequenceGenerator(name = "coupon_seq", sequenceName = "coupon_seq", allocationSize = 100)
  @Column(name = "coupon_id")
  private Long couponId;

  // getter & setters for couponId

}

Note: Same way store entity and storeId too configured.

All Tables: (2 sequences are created by hibernate for 2 entities store, coupon)

store table: (sequences are starting from 1 from store sequence)

store sequence: (Auto created / custom sequence)

coupon table: (sequences are starting from 1 from coupon sequence)

coupon sequence: (Auto created / custom sequence)

Things to Note:

3. GenerationType.AUTO

If you use GenerationType.AUTO then hibernate internally creates hibernate_sequence table to maintain the sequence number, the more important thing to consider is, it shares the sequence number across the tables.

Eg: if store_id is 1 then the coupon_id will start from 2 and 3 and hibernate_sequence table has the next value 4.

Coupon Entity: (same way create entity for Store also)

@Entity
public class Coupon {

  public Coupon() {

  }

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "coupon_id")
  private Long couponId;

  // getter & setters for couponId

}

Note: Same way store entity and storeId too configured.

All Tables: (3 tables – 2 entity tables + hibernate_sequence table by hibernate)

store table: (sequences are starting from 1 from hibernate sequence)

coupon table: (sequences are starting from 2 from hibernate sequence (shares between the tables))

hibernate_sequence: (Auto created by hibernate)

Things to Note:

Must to know Hibernate stuffs:

  1. Difference between session and sessionfactory hibernate?
  2. Understanding @Temporal and @JsonFormat in JPA Hibernate
  3. Hibernate All Annotations – Tutorials & Examples
  4. Understanding @JsonIgnore with JPA Hibernate
  5. Spring Boot + EhCache Hibernate Working Example
  6. Join two tables using Hibernate in Spring Boot Example
  7. How to set up Java Hibernate with MySQL in Eclipse ?
  8. How to create a sessionfactory in hibernate 4.3.6?
  9. Hibernate Join Annotation Example
Exit mobile version