Sequence Algorithm

Sequence Algorithm
  • This primary key generation algorithm is able to generate primary key value on the basis of the sequence provided by the underlying Database.
  • This algorithm is able to generate primary key values of the data types like short, int, long, etc.
  • This algorithm required “sequence” input parameter with the sequence name as value in order to generate primary key value.
  • If we have not provided any sequence name as input parameter then this algorithm is able to take “hibernate_sequence” as default sequence name, here developers must create “hibernate_sequence” in database explicitly.
  • This algorithm is supported by almost all the databases which are supporting sequences like Oracle, DB2, etc.
  • This algorithm is represented by Hibernate Software in the form of a short name like “sequence” and in the form of a predefined class like “org.hibernate.id.SequenceGenerator”.

E.g

In Database

SQL>create sequence my_sequence increment by 5;
SQL>commit;

Mapping file

<?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="sequence">
			    <param name="sequence">my_sequence</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>

Note

If we don’t want to use explicit sequence then we must create default sequence in database, that is, “hibernate_sequence”.

SQL>create sequence hibernate_sequence increment by 5;
Sequence Algorithm
Scroll to top