Primary Key Generation Algorithms in Hibernate

Primary Key Generation Algorithms in Hibernate

Hibernate software generates the primary key value dynamically by choosing an algorithm . We can configure primary key generation algorithm by using <generator> tag. This is sub tag of <ID> tag. In this post we are going to discussing what are the primary key generation algorithms in hibernate.

<?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="com.narayanatutorial.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>

Hibernate provides multiple algorithms

Algorithms

  1. increment
  2. assigned
  3. hilo ( high low )
  4. select
  5. native
  6. identity
  7. uuid

The default algorithm is assigned algorithm.

Here we are going discussing frequently using primary key generation algorithms

1) Assigned

It is default algorithm according to this algorithm the value assigned to the primary key field variable will become primary key value in the database.

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

2) increment

We can configure increment algorithm as follows

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

Increment algorithm works with all database software. But the data type of primary key field must be int, short, byte, long.

3) sequence

We can configure the sequence algorithm as follows.

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

If you are not specifying the sequence explicitly, the database software uses default sequence (+1) so each and every client request primary key increment +1.

We can set the our own sequence in the mapping file. For this first we have to create sequence in the database explicitly by using following code.

we will create sequence as follows.

create sequence my-sequence increment by 20

Configure our own sequence in the mapping file by using <param> tag. It is 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 is also applicable only on int, long, short, byte data types.

All database software does not support the sequence algorithm

Example

Oracle supports the sequence algorithm

MySQL does not support the sequence algorithm

4) hilo (high low)

This algorithm uses high low algorithm. It generates the hilo algorithm. It uses the database data column range.

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

Conclusion

When we use any primary key generation algorithm make sure that it works in all database software. For this it is recommended to assigned or increment algorithm.

 

 

Leave a Reply