GenerationType.IDENTITY vs GenerationType.SEQUENCE vs GenerationType.AUTO

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 make it fit in the right place.

Suppose you have two tables namely store and coupon, store id is a primary key in store table and foreign key in coupon table.

Store table: store_id | store_name | store_url

 

Coupon table: coupon_id | coupon_name | store_id

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:


@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:

 

Store table:

 

Coupon table:

 

Things to Note:

  • GenerationType.Identity does not create any additional sequence tables and also maintain the sequences in every table starts from 0, rather than maintaining the sequence number across the tables like GenerationType.AUTO does it.
  • GenerationType.AUTP generates one more table named hibernate_sequences for maintaining the sequences.
  • GenerationType.SEQUENCE is purely customizable, probably every auto generation field would have configured with separate sequences.

 

2. GenerationType.SEQUENCE

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:


@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:

 

store table:

 

store sequence:

 

coupon table:

 

coupon sequence:

 

Things to Note:

  • You may find the difficulty to understand the next_val of coupon and store sequences, please note this vary based on the¬†allocationSize attribute @SequenceGenrator. Here we specified it as¬†allocationSize = 100, so it returns 201.

 

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:


@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:

 

store:

 

coupon:

 

hibernate_sequence:

 

Things to Note:

  • Even though we did not specify any sequences hibernate creates it internally and maintains the next_val in the table named hibernate_sequences.

585 total views, 1 views today

Leave a Reply

Your email address will not be published. Required fields are marked *