Hibernate Generator classes

  • Facebook
  • Google
  • LinkedIn
  • Twitter

The generator classes generate an Id value for an object, which insert in database as a primary key. Hibernate provides different types of predefined generator classes, to represent the primary key column in the database.

All the generator classes implements org.hibernate.id.IdentifierGenerator interface. If anyone want own user defined generator class then they need to implement the org.hibernate.id.IdentifierGenerator interface and override the generate() method.

To configure generator class use <generator /> tag, which has one attribute called "class" where we can specify generator class-name.

Here are multiple pre-defined generator class are exist in hibernate.

  1. assigned
  2. increment
  3. sequence
  4. hilo
  5. seqhilo
  6. identity
  7. foreign
  8. native
  9. uuid

assigned

assigned class returns same id which set by the programmer to hibernate and hibernate will save an object with this Id in database. If there is no <generator> element for id in hbm.xml file then by default assigned generator is mapped to it.


<id name="employeeId" column="eid" /> 

OR 

<id name="employeeId" column="eid">
      <generator class="assigned"/>
</id> 

increment

This is used for generating the id value for the new record by using the formula max(id)+1. If there is no record available in the database, then for the first time this will saves primary key value as 1.


<id name="employeeId" column="eid">
     <generator class="increment"/>
</id> 		

sequence

This class is database dependent so we can not use this generator class for all the database it works with Oracle, DB2, SAP DB, Postgre SQL, etc, but does not work with MySql.

It reads next value of a database sequence and then returns that value as id to the hibernate. We need to configure the database sequence name as a parameter to the SequenceGenertor class. If we don't pass a database sequence name as a parameter, then SequenceGenerator class creates its own sequence with the name hibernate_sequence.


<id name="employeeId" column="eid">
    <generator class="sequence"/>
     <param name="sequence">my_sequence</param>
</id> 		

SequenceGenerator class selects next value of my_sequence from database. If my_sequence does not exist in database then it will throw an Exception.

Or


<id name="employeeId" column="eid">
    <generator class="sequence"/>
</id> 		

SequenceGenerator class creates its own sequence with hibernate_sequence name and returns the next value of that sequence to the Hibernate.

hilo

TableHiloGenerator class returns id as 1 for the 1st time and uses a table to store the HiLo high value, this is also database independent generator so it will work for all the databases. This class accepts the parameters table, column and max_lo as in below example.


<id name="employeeId" column="eid">
    <generator class="hilo"/>
       <param name="table"/>mytable</param/>
       <param name="column"/>col1</param/>
       <param name="max_lo"/>10</param/>
</id> 		

If we do not configure the above 3 parameters then hibernate will give the default values to them as below.

table: the defaults table name is 'hibernate_unique_key'

column: the defaults column is 'next_hi'

max_lo: the defaults max_lo is 32,767

Here is the formula to generate Id and it always generates first id as "1". After that the next Id onwards will generate by this formula.

Hilo formula: max_lo * next_hi +next_hi

seqhilo

It is similar to hilo generator class, But hilo generator stores its high value in table, but seqhilo generator stores its high value in the sequence.


<id name="employeeId" column="eid">
    <generator class="seqhilo"/>
       <param name="sequence"/>eid-seq</param/>
       <param name="max_lo"/>10</param/>
</id> 		

identity

IdentityGenerator class call auto-increment algorithm of a database it reads the incremented value and returns that value as id to the Hibernate and this algorithm does not exist for all the databases, it exist in Sybase, My SQL, MS SQL Server, DB2 and HypersonicSQL, but it does not exist in Oracle. So, Identity is an database dependent generator.


<id name="employeeId" column="eid">
    <generator class="identity"/>
</id> 		

native

native generator works as either sequence or identity or hilo. Hibernate first calls SequenceGenerator if database support the sequence then it acts as sequence genetaror, if not supported by database, then it calls IdentityGenerator and acts as identity generator, if not supported then finally TableHiloGenerator is called and acts as hilo generator. Native is a database independent generator.


<id name="employeeId" column="eid">
    <generator class="native"/>
</id> 		

foreign

foreign generator is used only in one-to-one relationship.


<id name="employeeId" column="eid">
    <generator class="foreign"/>
</id> 		

ForeignGenerator class returns id of parent object as id for child object. Foreign generator is a database independent generator.

uuid

uuid generates a identifiers of type string as a primary key, based on the following 4 values

  1. IP Address.
  2. JVM Start Up time.
  3. Current System time.
  4. Counter value in the JVM.

<id name="employeeId" column="eid">
    <generator class="uuid"/>
</id> 		

      Advertisements