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
  } 

Related Topics

Hibernate One-to-Many Mapping

One-to-Many Hibernate Mapping Example In One-to-many association mapping, only one object of a persistent class is related to many objects of another persistent class. It is a relationship in which one (parent) is related...

3 minutes read.

Hibernate Session Interface

Hibernate Session Interface A Session is an interface between the Java application and the Hibernate. It is available in the org.hibernate.session package. It should not be kept open for a long time, as it...

4 minutes read.

Hibernate One-to-One Mapping Example

One-to-One Hibernate Mapping In One-to-One association mapping, one object of a persistent class is related to one object of another persistent class. Here, we are going to create an example of one-to-one mapping using...

3 minutes read.

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...

3 minutes read.

Hibernate Features

1. Lightweight Hibernate is a lightweight framework as it does not contains additional functionalities; it uses only those functionalities required for object-relational mapping. It is a lightweight framework because it uses persistent...

2 minutes read.

Hibernate Mapping

Hibernate Mapping Association- It means that two or more things are related to each other by some specific relation. In other words, it specifies how the objects are associated with each other. Hibernate mapping is one...

2 minutes read.

Hibernate N+1 Select Problem

Hibernate N + 1 Select Problem The N + 1 Select problem is a performance issue in Hibernate. In this problem, a Java application makes N + 1 database calls (N = number...

4 minutes read.

Hibernate Inheritance - Single Table

Hibernate Inheritance In this inheritance strategy, only one table is created for all the classes involved in the hierarchy with an additional column known as a discriminator column. The discriminator column helps indifferentiating between...

3 minutes read.

Architecture of Hibernate

Next → Hibernate is an ORM framework with a layered architecture. It consists of many objects, such as a persistent object, sessionfactory object, transaction object, session, etc.  There are mainly three layers in Hibernate...

2 minutes read.

Cascade in Hibernate

Cascading is a feature in Hibernate, which is used to manage the state of the mapped entity whenever the state of its relationship owner (superclass) affected. When the relationship owner (superclass) is saved/...

3 minutes read.

Dirty Checking in Hibernate

Dirty checking is an essential concept of Hibernate. The Dirty checking concept is used to keep track of the objects. It automatically detects whether an object is modified (or not) or wants to...

6 minutes read.

Hibernate Annotation with Example

Hibernate is an ORM (Object-Relational Mapping) tool which simplifies the application development. Hibernate provides a framework which interacts with the data stored in the databases and it also uses the specifications of the...

2 minutes read.

Hibernate Second-Level Cache

The Second-level cache is related to the SessionFactory object. Once the SessionFactory is closed, all the second-level cache data associated with it will be lost. The cache manager will also get closed. Cache provider The...

5 minutes read.

Hibernate First-Level Cache

The first-level cache is related to the Session object. First level cache objects of one Session are not visible to the other Sessions, and all the cache data is lost when the Session...

2 minutes read.

Hibernate Configuration Properties

Hibernate Configuration Hibernate Configuration is a Java class, which allows a Java application to specify configuration parameters used in the application. As Hibernate is designed to serve in different environments, it needs a broad...

6 minutes read.

Hibernate Inverse

Hibernate Inverse An Inverse attribute is used to maintain the relationship between the parent and child class object. The inverse attribute is used only with bi-directional mappings such as one-to-many and many-to-many Hibernate mapping. In Hibernate, the...

2 minutes read.

Annotation Example in Hibernate

Hibernate Annotation Example Hibernate is an ORM (Object-Relational Mapping) tool which simplifies the application development. Hibernate provides a framework which interacts with the data stored in the databases, and it also uses...

3 minutes read.

Hibernate merge() vs. update()

In Hibernate, both update() and merge() methods are used to convert the detached state object into the persistent state. session.merge() The merge() method is used when we want to change a detached entity into...

2 minutes read.

Hibernate Named Query Using XML

A named query is a technique used to assemble all the queries (Native SQL and HQL) in a specific location and refer them by some name. It helps in reducing the mess...

3 minutes read.

Hibernate Many-to-One Mapping

Many-to-One Hibernate Mapping with Example A Many-to-One association mapping is the reverse of One-to-Many association mapping. For example, many (customers) are associated with one (vendor). In Hibernate, Many-to-One association mapping is applied from child class...

3 minutes read.