hibernate generators

Posted: April 21, 2012 in Hibernate

Hibernate software provides set of algorithms to generate identity values

1)      Assigned

2)      Increment

3)      Identity

4)      Native

5)      Sequence

6)      Hilo

7)      Seq hilo

8)      Guid

9)      Uuid

10)   Select

11)   Foreign


This algorithm uses programmer assigned value in primary key filed member variable of given pojo class object as identity value of the object

For every identity value generator algorithm, there will be a class and shortcut name. The class name of assigned algorithm is “org.hibernate.id.Assigned” and short cut name is assigned. All the classes that are acting as identity value generators implement a common interface called “org.hibernate.id.IdentifierGenerator”

<id name=”sno” column=”eid”>

<generator class=”org.hibernate.id.Assigned” or “assigned”/></id>

<id name=”sno” column=”eid”/> uses assigned as default algorithm, since no other

algorithm is explicitly specified.


In order to choose identifier generator algorithm we must consider the following two operations

a)      Ensure that the algorithm generated value is compatible with data type of identify field

b)      Ensure that underlying DB s/w is having provisions to support the algorithm

The algorithm assigned is compatible with all data types and supported by all database software

Increment Algorithm:

This algorithm generated identity values of type long or short or int that are unique. This algorithm works with all database software but can     generate only numeric values as identity values. The class name of this algorithm is “org.hibernate.id.IncrementGenerator”. This uses “(Max identity value + 1) as the formula to generate next identity value.

<id name=”sno” column=”eid”>

<generator class=”increment” (org.hiberante.id.IncrementGenerator)/> </id>

Sequence Algorithm: (doesn’t work with mysql)

This algorithm uses sequence concept available in DB2, postgreSQL, oracle, SAPDB …, and it generates identity values of type long,short,int. the class name of this algorithm is “org.hibernate.id.SequenceGenerator”.  To specify explicitly created sequence in database software as input values to this algorithm, use <param> having parameter name & value.

The sequence algorithm works only with the above listed database software


i)    Create sequence in underlying database software.

SQL> create sequence my_seq increment by 2;

ii)   Configure sequence algorithm by passing the sequence name as parameter value to the algorithm

<id name=”sno” column=”eid”>

<generator class=”sequence (or) org.hibernate.id.SequenceGenerator”>

<param name=”sequence”>my_seq</param>


Identity algorithm:

This algorithm use identity value generated by identity columns available in DB2, mysql, SQL server, Sybase database software. This algorithm returns identifier value of type Long, short or int.

Identity column means the column that generates that’s its next record value by sing “Max value+1” formula

Note: To make ordinary column as identity column in mysql database software, we need to apply auto increment constraint on the table column. To apply increment constraint the column must be numeric data type column and the column should have primary key constraint.

The class name of this identifier algorithm is “org.hibernate.id.IdentityGenerator”

<id name=”sno” column=”eid”>

<generator class=”identity”/></id>

With respective to the above code the EID column of the table should act as identity column by having primary key, auto increment constraints.

Difference between increment and identity algorithm:

  • While working with increment algorithm the id field related table column need not to have any constraint [constraints are optional]. Whereas working with identity algorithm the id field related table column must having primary key, auto increment constraint.
  • Hibernate software generates increment algorithm based identity value.
  • The database software generates identity algorithm based identity value on identity column and hibernate software collects & uses that value.
  • Increment algorithm works with all database software
  • Identity algorithm works with only database software which support identity column (mysql, DB2 …)

Native algorithm:

This algorithm is not having its own individual behavior. This algorithm is capable of picking up identity, sequence or hilo algorithm based on the capabilities of underlying database software.

Due to this, algorithm works with all database software. While working with this algorithm, passing parameters is optional. But we can pass all the regular prams of sequence, hilo algorithms.

When native algorithm is configured by taking mysql underlying database software then it looks to work with identity algorithm.

When native algorithm is used by taking oracle as the underlying database software then  it use sequence algorithm to generates identity value.

If underlying database software does not support identity columns and sequence then the native algorithm looks to use hilo algorithm to generate id value.


<id name=”no” column=”eid”>

<generator class=”native”/>


In the above code native algorithm uses identity algorithm

<id name=”no” column=”eid”>

<generator class=”native”>

<param name=”sequence”>my_seq</param>



In the above code native algorithm uses sequence algorithm internally

<id name=”sno” column=”eid”>

<generator class=”native”>

<param name=”table”>mytable</param>

<param name=”column”>mycol</param>

<param name=”max_lo”>10</param>


In the above code native algorithm uses hilo algorithm internally.


In order to work with native algorithm first try to notice which algorithm it is using internally for the underlying database software. Based on this you start configuring parameters while working with native algorithm.

Universal Unique Id Algorithm: (UUID)

This algorithm generates identifiers of type string which is unique with in a network (uses IP address). This algorithms use identifier values as encoded string of hexadecimal digits of length 32. This algorithm works with all database software. The class name of this algorithms is “org.hibernate.id.UUIDHexGenerator”. This algorithm generated id value is dynamic & random value.

Example: set up required [changes must be done in the following areas]

1)   Desc employee;

Field                               type

Firstname               varchar(20)

Lastname               varchar(20)

Email                       varchar(20)

2)   Hibernate pojo class (EmpBean)


Private String no;

Private String fname,lname,mail;


3)      Coding mapping file

<id name=”no” column=”eid”>

<generator class=”uuid”/>



Global Unique Id Algorithm (guid)

This algorithm uses “new id()” available in sql to generate string based dynamic & random identifier value. This algorithm works only with mysql server & mysql database software. The class name of this algorithm is “org.hibernate.id.GUIDGenerator”


Uuid algorithm makes hibernate software to generate String based id value. GUID algorithm make underlying database software to generate id value and hibernate software uses that value.

Select Algorithm:

This algorithm take the database trigger generated value as id value of hibernate pojo class object. The class name is “org.hibernate.id.SelectGenerator”

Foreign Algorithm:

This algorithm is useful to generate id value in one pojo class object by collecting identity value form another pojo class object. This algorithm is very useful in one-to-one association mapping. The class name of this algorithm is “org.hibernate.id.ForeignGenerator”

Hilo (high and low) algorithm:

This algorithm generates identifiers of type long, short, int by using given table and its column as the source of ‘high’ values. The class name of this algorithm is “org.hibernate.id.TableHiLoGenerator”

This algorithm requires 3 mandatory parameters. Table, column, Max_lo

The column in the special table keeps track of the number of records that are inserted using this HiLo algorithm.

When multiple identity values are generated using this Hilo algorithm, the difference between each two values is (“Max_lo value+1”). This algorithm works with all database software.

Example: SQL> select * from mytable;


400   —-> you can insert any value

<id name=”sno” column=”eid”>

<generator class=”hilo”>

<param name=”table”>mytable</param>

<param name=”column”>mycol</param>

<param name=”max_lo”>10</param>


Note: The first id value that has come through this algorithm is dynamic and random value. For this algorithm table must be empty.

Seqhilo algorithm:

This algorithm uses given database sequence and max_lo parameter value to generate identifiers of type long, short or int. This algorithms works with only in those database software which supports sequence

This algorithm having 2 mandatory parameters



When multiple values (identity) are generated using this algorithm, the difference between two id values will be [increment by value of (sequence) * max_lo value] + [increment value of (sequence)]

The first id value generated by this algorithm is random value.

<id name=”sno” column=”eid’>

<generator class=”seqhilo”>

<param name=”sequence”>my_seq</param>

<param name=”max_lo”>10</param>




  • If identity filed member variable data type is numeric data type then use increment (or) native algorithms.
  • If identity filed data type is other than numeric data type use assigned algorithm and generate id value manually.



  1. Prashant says:

    Good Explanation

  2. loknath says:

    very clear explanation. Thank you gopal

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s