Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

Using Hibernate to generate id incrementally

As we have seen in the last section that the increment class generates identifiers of type long, short or int that are unique only when no other process is inserting data into the same table.


When should you use Hibernate? What's new in Hibernate 5? How much time it takes to learn Hibernate? Hibernate envers custom revinfo table Hibernate 5 Envers Hibernate Envers get all revisions Hibernate Examples Hibernate 5 build SessionFactory Example Hibernate 5 SessionFactory Example Hibernate 5 Annotation Example How to download Hibernate 5.x? Hibernate 5 Features Hibernate 5 Tutorials Learning Hibernate ORM - Free Training/Tutorial videos of Hibernate ORM with Java How to write hello world example program in Hibernate? How to create one to one relationship in Hibernate 4 with Annotation? How to calculate minimum in Hibernate using the min() Function Hibernate ORM 4.2.21.Final Released - Create CURD application in latest Hibernate Hibernate ORM 4.2.21 Tutorial - Learn Hibernate ORM Framework Hibernate Error: Failed to create sessionFactory How to create Hibernate Project in Eclipse? Examples of Hibernate Criteria Query Hibernate ORM 4.3.6.Final released Hibernate ORM 4.2.14.Final released Hibernate 4.3.4 Released: Hibernate 4.3.4.Final Maven dependency Hibernate association and join example Learn Hibernate programming with Examples Introduction to Hibernate Framework Hibernate Tutorial: Learn Hibernate with examples Hibernate 4.3 Hello World: Example Hibernate ORM 4.3.1.Final released How to create SessionFactory in Hibernate 4.3.1? Hibernate 4 Hello World: Example of Hello World program Hibernate 4 create Session Factory: Example of creating Session Factory in Hibernate 4 Hibernate 4.3 and JPA 2.1 Example What is Hibernate in Java? Hibernate Overview and Architecture What is JPA 2.1? Hibernate 4.3.0.Final Maven dependency Hibernate 4.3 ORM Features Hibernate 4.3 Tutorial Hibernate Criteria average example Hibernate Criteria load all objects from table Hibernate Criteria Hibernate ORM 4.3.0.Final released Hibernate beginner tutorial Hibernate Session.delete() Example Hibernate save or update method example Hibernate Load Example Hibernate save Example - Rose India Hibernate 4 tutorials

Using Hibernate <generator> to generate id incrementally


As we have seen in the last section that the increment class generates identifiers of type long, short or int that are unique only when no other process is inserting data into the same table. In this lesson I will show you how to write running program to demonstrate it. You should not use this method to generate the primary key in case of clustured environment.

In this we will create a new table in database, add mappings in the contact.hbm.xml file, develop the POJO class (, write the program to test it out.

Create Table in the mysql database:
User the following sql statement to create a new table in the database.
 CREATE TABLE `book` ( 
`id` int(11) NOT NULL default '0', 
`bookname` varchar(50) default NULL, 

Developing POJO Class ( is our POJO class which is to be persisted to the database table "book".

 @author Deepak Kumar
 * Java Class to map to the database Book table
package roseindia.tutorial.hibernate;

public class Book {
  private long lngBookId;
  private String strBookName;
 @return Returns the lngBookId.
  public long getLngBookId() {
  return lngBookId;
 @param lngBookId The lngBookId to set.
  public void setLngBookId(long lngBookId) {
  this.lngBookId = lngBookId;
 @return Returns the strBookName.
  public String getStrBookName() {
  return strBookName;
 @param strBookName The strBookName to set.
  public void setStrBookName(String strBookName) {
  this.strBookName = strBookName;

Adding Mapping entries to contact.hbm.xml
Add the following mapping code into the contact.hbm.xml file

<class name="roseindia.tutorial.hibernate.Book" table="book">
   <id name="lngBookId" type="long" column="id" >
  <generator class="increment"/>

  <property name="strBookName">
   <column name="bookname" />

 Note that we have used increment for the generator class. *After adding the entries to the xml file copy it to the bin directory of your hibernate eclipse project(this step is required if you are using eclipse).

Write the client program and test it out
Here is the code of our client program to test the application.

 @author Deepak Kumar
 * Example to show the increment 
class of hibernate 
generator element to 
 * automatically generate 
the primay key

package roseindia.tutorial.hibernate;

//Hibernate Imports
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class IdIncrementExample {
  public static void main
String[] args) {
  Session session = null;

  // This step will read 
hibernate.cfg.xml and 
prepare hibernate for use

  SessionFactory sessionFactory = 
new Configuration().configure()
  session =sessionFactory.openSession();
  org.hibernate.Transaction tx = 
  //Create new instance 
of Contact and set values in
 it by reading them from form object

Inserting Book object 
into database.."
  Book book = new Book();
  System.out.println("Book object 
persisted to the database."
  }catch(Exception e){

To test the program Select Run->Run As -> Java Application from the eclipse menu bar. This will create a new record into the book table.


Share on Google+Share on Google+

Using Hibernate to generate id incrementally

Posted on: January 18, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Using Hibernate to generate id incrementally   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
Dennis Castillo
September 30, 2011

thanks to this post, i was able to solve a problem with a autoincrement id created after generate the pojo class.