Hibernate GeneratedValue Strategies

Hibernate GeneratedValue Strategies

Hibernate supports some generation strategies to generate a primary key in the database table. We can access the strategy with the help of @GeneratedValue annotation.

@GeneratedValue

The @GeneratedValue annotation specifies how to generate values for the given column. This annotation will help in creating primary keys values according to the specified strategy. The only thing we need to do is to add @GeneratedValue annotation in the POJO class.

Syntax of @GeneratedValue

@Id 
 @GeneratedValue  
 @Column(name="c_id")   
 private int cid;  

If we don’t use this annotation, the application is responsible for managing the @Id field values itself.

The @GeneratedValue annotation uses two parameters, strategy, and GenerationType. The GenerationType is an enum which defines the types of strategies. The strategies are listed below:

  • GenerationType.AUTO
  • GenerationType.IDENTITY
  • GenerationType.SEQUENCE
  • GenerationType.TABLE
Hibernate GeneratedValue Strategies

GenerationType.AUTO

The GenerationType.AUTO indicates that the persistence provider should select an appropriate strategy based on the specified database dialect. The AUTO will automatically set the generated values. It is the default GenerationType. If no strategy is defined, it will consider AUTO by default. All the databases support this strategy.

Syntax of GenerationType.AUTO

@GeneratedValue(strategy=GenerationType.AUTO)

Following is the example of this strategy:

@Entity
 @Table(name="customers")
 public class Customer {
  @Id 
  @GeneratedValue(strategy=GenerationType.AUTO)
  @Column(name="cust_id")
  private int cid;
  @Column(name="cname") 
  private String cname;
  // setters and getters
  } 

GenerationType.IDENTITY

The GenerationType.IDENTITY indicates that the persistence provider must assign primary keys for the entities using a database identity column. It is very similar to AUTO generationType. This strategy is not available in all the databases. If the particular database doesn’t support IDENTITY generationtype, the persistence provider will itself choose an alternative strategy.  

Syntax of GenerationType.IDENTITY

@GeneratedValue(strategy=GenerationType.IDENTITY)

Following is the example of this strategy:

@Entity
 @Table(name="customers")
 public class Customer {
  @Id 
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  @Column(name="cust_id")
  private int cid;
  @Column(name="cname")
  private String cname;
  //getters and setters 
  } 

GenerationType.SEQUENCE

The GenerationType.SEQUENCE indicates that the persistence provider must assign primary keys for the entities using the database sequence. This strategy consists of two parts, defining a sequence name and using the sequence name in the classes. It also builds a sequence generator in the database and not supported by all the databases. A SEQUENCE GenerationType is global to the application; it can be accessed by one or more entities in the application.

Syntax of GenerationType.SEQUENCE

@GeneratedValue(strategy=GenerationType.SEQUENCE)

Following is the example of this strategy:

@Entity
 @Table(name="customers")
 public class Customer {
  @Id
  @GeneratedValue(strategy=GenerationType.SEQUENCE)
  @Column(name="cust_id") 
  private int cid;
  @Column(name="cname")
  private String cname;
  //getters and setters
  } 

GenerationType.TABLE

The GenerationType.TABLE indicates that the persistence provider must assign primary keys for the entities using a database table. It uses a database table to store the latest primary key value. This strategy is available in all the databases.

Syntax of GenerationType.TABLE

@GeneratedValue(strategy=GenerationType.TABLE)

Following is the example of this strategy:

@Entity
 @Table(name="customers")
 public class Customer {
  @Id
  @GeneratedValue(strategy=GenerationType.TABLE)
  @Column(name="cust_id") 
  private int cid;
  @Column(name="cname")
  private String cname;
  //getters and setters
  }