GenerationType.IDENTITY vs GenerationType.SEQUENCE vs GenerationType.AUTO
Table of Contents
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,
- GenerationType.IDENTITY
- GenerationType.SEQUENCE
- 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
- GenerationType.Identity does not create any additional sequence tables like GenerationType.AUTO / GenerationType.SEQUENCE 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.AUTO 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.
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:
- GenerationType.Identity does not create any additional sequences / sequence tables.
- Every table/entity created with GenerationType.IDENTITY starts its value from 0.
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:
- 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: (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:
- Even though we did not specify any sequences hibernate creates it internally and maintains the next_val in the table named hibernate_sequences.
Must to know Hibernate stuffs:
- Difference between session and sessionfactory hibernate?
- Understanding @Temporal and @JsonFormat in JPA Hibernate
- Hibernate All Annotations – Tutorials & Examples
- Understanding @JsonIgnore with JPA Hibernate
- Spring Boot + EhCache Hibernate Working Example
- Join two tables using Hibernate in Spring Boot Example
- How to set up Java Hibernate with MySQL in Eclipse ?
- How to create a sessionfactory in hibernate 4.3.6?
- Hibernate Join Annotation Example