Native Algorithm

Native Algorithm

Native algorithm is able to generate primary key value by selecting a particular primary key generation algorithm depending on the database which we used.

This algorithm is not having its own algorithm to generate primary key value. It will select “SequenceGenerator” algorithm if we are using Oracle database, “IdentityGenerator” algorithm if we are using MySQL database and “TableHiLoGenerator” if we are using some other database which is not supporting SequenceGenerator and IdentityGenerator.

This Primary key generator is able to generate primary key values of the data types like short, int, long, etc.

This primary key generator is supported by almost all the databases.

To represent this mechanism, Hibernate has provided a short name in the form of “native”, Hibernate has not provided any predefined class.

This algorithm is able to take parameters on the basis of the selected primary key generator depending on the database. If it is using “SequenceGenerator” then we must provide “sequence” parameter. If it is using TableHiloGenerator then we have to provide “table”, “column”, “max_lo” parameters.

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="native">
			    <!-- Provide parameters depending on requirement-->
			</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>
Native Algorithm


Scroll to top