All IT Courses 50% Off
JAVA Tutorials

What is Hibernate Algorithm for the primary key?

Hibernate <Generator> classes are used to generate the primary key or an identifier. Multiple algorithms are used to generate the primary key. <generator> tag is a subtag of <id> tag. All the generator classes implement interface and it overrides the generate(SessionImplementor,Object) method. If you want to define your user-defined generator, you should implement the IdentiferGenerator interface and override the generate().

The basic syntax is :

<generator class="">
<param name=""> value </param>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "">
  <class name="hibernate.Student" table="student">
      <id name="hid" column="HID" type="integer">
      <generator class="increment"></generator>
        <property name="sfirstname" column="SFIRSTNAME" type="string"></property>
        <property name="slastname" column="SLASTNAME" type="string"></property>
        <property name="srollnumber" column="SROLLNUMBER" type="integer"></property>

Hiberanate provides multiple algorithms such as :

  • Increment
  • Assigned
  • Sequence
  • Hilo (High Low)
  • Select
  • Native
  • Identity
  • Uuid
  • Seqhilo
  • Guid
  • Foreign
  • Sequence-identity

1] assigned: It is the default algorithm. If no <generator> tag is defined then assigned generator class is used. In this algorithm the value assigned to the primary key field will be the primary key value in the database.

<id name="hid" column="HID" type="integer">
     <generator class="assigned"></generator>

2] increment: This algorithm works with all types of database software but the data type of primary key should be int, short, byte or long.

<id name="hid" column="HID" type="integer">
     <generator class="increment"></generator>

3] sequence: If the sequence is not explicitly defined the database uses default sequence (+1) and every client request is then increment by +1.

All IT Courses 50% Off

You can also create your own sequence by explicitly creating the sequence in the database as follows:

create sequence my-sequence increment by 20

You can also configure the sequence by using <param> tag which is a child tag of <generator> tag.

<id name="hid" column="HID" type="integer">
<generator class="sequence">
<param name="sequence" value="my-sequence"></param>

Sequence algorithm can only be applied to int, short, long and byte data types.

Sequence algorithm is not supported by all the database softwares. For example:

Oracles supports sequence algorithm but MySQL does not supports sequence algorithm. If there is no sequence in the database then default sequence i.e. HIBERNATE_SEQUENCE is used.

4] hilo (high low): It uses high low algorithm and it uses database data column range.

<id name="hid" column="HID" type="integer">
     <generator class="hilo"></generator>

Default table is hibernate_unique_key and column is next_hi.

5] native: It uses identity, high low or sequence algorithms depending on the database vendor.

<id name="hid" column="HID" type="integer">
     <generator class="native"></generator>

6] identity: It is used in MySQL, MS SQL Server, Sybase, DB2 to support the id column. Database is responsible to generate the unique identifier.

<id name="hid" column="HID" type="integer">
     <generator class="identity"></generator>

7] seqhilo: It uses sequence and hilo algorithms. Its return data type is short, int , long.

<id name="hid" column="HID" type="integer">
     <generator class="seqhilo"></generator>

8] uuid: For the generation of id it uses 128-bit UUID algorithm. The UUID is represented as hexadecimal digits having length of 32 characters. It generates identifier using IP Address. It is also used to generate password.

<id name="hid" column="HID" type="integer">
     <generator class="uuid"></generator>

9] guid: It only works on MySQL and MS SQL Server.

10] select: It uses the primary key value returned through database trigger.

11] foreign: It is mostly used in one-to-one association and uses id of another associated object.

12] sequence-identity: It is only supported in Oracle 10g drivers and uses a special sequence generation strategy.

Facebook Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Articles

Back to top button