17
Jan

What are Generators in Hibernate?

Hibernate being a high-quality Object Relational Mapping (ORM) not only takes care of the mapping from Java classes to database tables (and from Java data types to SQL data types) but also provides data query and retrieval facilities.

Generators in Hibernate play a great role. The class is a sub-element of id. This is used to generate the unique identifier for the objects of the persistent class. There are many generator classes defined in the Hibernate Framework.

Example of Generator

<generator class=””>

<param name=””> value </param>

</generator>

This is how Generators actually work. Hibernates provide different primary key generator algorithms.

What is the List of Generators?

When it comes to Hibernate, these are the most common list of generators classes.

assigned
increment
sequence
identity
hilo
native
foreign
uuid.hex
uuid.string

In the above list, the first 7 are used for int, long, short types of primary keys, and last 2 are used when the primary key column type is String type.

Let us have a detailed discussion on each generator class in this article for a clear perception.

Assigned

This is the default generator strategy. If there is no element being defined, Assigned is considered to support all the databases.
To make it clear, refer to this example:

<hibernate-mapping>  

 <class …>  

   <id …>  

    <generator class=”assigned”></generator>  

   </id>         

 </class>  

</hibernate-mapping>  

If a generator class is assigned, the programmer is responsible for assigning a primary key value to the object which will be stored in the Database.

Increment

This is a Database-Independent class which generates a unique id if there is no other process inserting the data into the table.
It generates an id value for a new record by the formula:

Max of id value in Database + 1

If the value is manually assigned, then hibernate doesn’t consider that value and uses a max value of id in database + 1 concept only.
If there is no record initially in the database, then for the first time it will save as primary key value object as 1.
This example will make it clear:

<hibernate-mapping>  

 <class …>  

   <id …>  

    <generator class=”increment”></generator>  

   </id>  

 </class>  

</hibernate-mapping>  

Sequence

As the name suggests, it uses a sequence of the database. It doesn’t have support with MySQL.
This is Database Independent which means that we cannot use the generator class for all the database.
This is how it works:

<id name=”productId” column=”pid”>
<generator>
<param name=”sequence”>MySequence</param>
</genetator>
</id>

If the programmer hasn’t assigned any sequence, the application itself assigns a sequence named “Hibernate-Sequence” and gets next value from that sequence, and then assigns that id value for the new record.

Identity

This is a Database Independent class which is not working in Oracle.
Here the value is generated primarily by the database, not by the hibernate.
This is similar to the Increment Generator, but the main difference was increment generator is database independent and hibernate uses a select operation for selecting max of id before inserting a new record.
This is the Syntax of Identity class:

<id name=”productid” column=”pid”>

<generator class=”…….”/>

</id>

Hilo

This is also a Database Independent class whose id value will be inserted as 1 for the first record.
If we need to modify the tables and column names, we need to pass 2 parameters for Hilo Generators.
Here is an example:

<id name=”productId” column=”pid”>

<generator>

<param name=”table”>your table name</param>
<param name=”column”>your column name </param>

</generator>

</id>

Native

This class initially checks whether it supports the database or not.
It generates the identifiers based on table and column values in the tag.
Default table is ‘hibernate_unique_key’ and column is ‘next_hi‘.
hilo generator configuration in hibernates mapping file.
The best example is:

<id name=”empId” column=”EMPNO”>
<generator class=”hilo”>
<param name=”table”>HIGH_VAL_TABLE</param>
<param name=”column”>HIGH_VAL_COLUMN</param>
<param name=”max_lo”>60</param>
</generator>
</id>

foreign

foreign uses the identifier of another associated object.
Usually used in conjunction with a primary key association.

uuid.hex

It uses a 128-bit UUID algorithm to generate the identifiers of type string.
It generates the identifiers using the IP address. The generated identifier is unique within the network.
It is also used to generate passwords.
This will be the best example:

<id name=”empId” column=”EMPNO”>
<generator class=”uuid”>
</generator>
</id>

OR

<id name=”empId” column=”EMPNO”>
<generator class=”org.hibernate.id.UUIDHexGenerator”>
</generator>
</id>

uuid.string

It uses a 128-bit UUID algorithm to generate the id.
The returned id is of type String, unique within a network (because IP is used).
The UUID is represented in hexadecimal digits, 32 in length.

Final Words

These Generators are going to help you with Hibernate. Hope you found this interesting!

If you have any queries jot down your comments in the comments section below!

Happy Reading!

Comments

[bws_google_captcha]