What is Hibernate Algorithm for the primary key

What is Hibernate Algorithm for the primary key?

Table of Contents

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 hibernate.id.IdentifierGenerator 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>
</generator>
Example:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <class name="hibernate.Student" table="student">
      <id name="hid" column="HID" type="integer">
      <generator class="increment"></generator>
        </id>
        <property name="sfirstname" column="SFIRSTNAME" type="string"></property>
        <property name="slastname" column="SLASTNAME" type="string"></property>
        <property name="srollnumber" column="SROLLNUMBER" type="integer"></property>
  </class>
</hibernate-mapping>

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>
</id>

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>
</id>

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

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>
</generator>
</id>

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>
</id>

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>
</id>

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>
</id>

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>
</id>

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>
</id>

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.

One Response

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.

Share this article
Subscribe
By pressing the Subscribe button, you confirm that you have read our Privacy Policy.
Need a Free Demo Class?
Join H2K Infosys IT Online Training
Enroll Free demo class