Hilo Algorithm

Hilo[High-Low] Algorithm

Hilo algorithm is able to generate primary key value by getting high value from a particular global resource and low value from mapping file.

Note

Global resource is a particular table with a column in the same database.
This algorithm is able to generate primary key values of the data types like short, int, long, etc

To represent this algorithm. Hibernate software has provides a separate short in the form of “hilo” and a separate predefined class in the form of “org.hibernate.id.TableHiLoGenerator”.

This algorithm is supported by almost all the databases like Oracle, MySQL, etc

To generate primary key value this algorithm will take the following three parameters.

  1. table: Global resource name, that is, a table name which provides high value.
  2. column: column name existed in Global Resource to manage high value.
  3. max_lo: It will provide low value.

Note

If we have not provided Global resources parameters like table and column in mapping file then Hibernate Software will take high value from default global resource table “hibernate_unique_key” and column “next_hi”.

E.g

<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
	<class name="com.ashok.hibernate.sample.model.Employee" table="emp">
		<id name="id" type="int" column="id">
			<generator class="hilo">
			    <param name="table">my_table</param>
			    <param name="column">my_column</param>
			    <param name="max_lo">10</param>
			</generator>
		</id>
		<property name="empName" column="emp_name" type="string" />
		<property name="address" column="address" type="string" />
		<property name="salary" column="salary" type="float" />
	</class>
</hibernate-mapping>

In database

SQL>create table my_table(my_column number primary key);
SQL>insert into my_table values(100);
SQL>commit;

Note

If we want to use default table name and column instead of my_table and my_column then we have to remove “table” and “column” parameters in mapping file and provide the following in Database.

SQL>create table hibernate_unique_key(next_hi number primary key)
SQL>insert into hibernate_unique_key values(100);
SQL>commit;
Hilo Algorithm
Scroll to top