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 Inheritance Table Per Class

In this inheritance strategy, table per class is generated. It means a separate table is generated for each POJO class involved in the hierarchy. Unlike the SINGLE_TABLE strategy, there are no nullable values...

3 minutes read.

Hibernate Web Application Example

Web Application Example with Hibernate In this section, we create a hibernate web Application. Here we are using a JSP file for presentation. Example of creating a web application using hibernate index.jsp This page will show a...

2 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 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 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 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 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 SessionFactory

Next → Hibernate SessionFactory A SessionFactory is an interface in Hibernate that create the instances of Session interface. It is a heavy weight object, and it is usually created during the application...

3 minutes read.

Hibernate vs. JDBC

What is Hibernate? Hibernate is an Object-Relational Mapping (ORM) tool which reduces the difficulty in the application development. Hibernate provides a framework that interacts with the data stored in the databases. It also uses...

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 Criteria Query Language (HCQL)

Hibernate Criteria Query Language (HCQL) is mainly used for searching and fetching records. It works on the filtration rules and logical conditions. The Criteria interface, Restriction class, and Order class are available in...

4 minutes read.

Hibernate get() vs. load()

Hibernate methods, get() and load() both are provided by the Session interface and used to retrieve the objects from the database. Session.get() The session.get() method is used to fetch a persistent object of the...

2 minutes read.

Hibernate vs. JPA

What is JPA? JPA stands for Java Persistence API, which defines a set of functionalities, standards, and concepts to the ORM tool. It is a framework used to manage relational databases. It is available...

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 Tutorial

What is Hibernate? Hibernate is an Object-Relational Mapping (ORM) tool which reduces the difficulty in the application development. Hibernate provides a framework that interacts with the data stored in the databases. It also uses...

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

Lazy Loading in Hibernate

Lazy loading is a fetching technique used for all the entities in Hibernate. It decides whether to load a child class object while loading the parent class object. When we use association mapping...

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