Hibernate-generator-element

Understanding Hibernate <generator> element

  • In hibernate generator classes are used to generate unique identifiers for instances of the persistence class.
  • Hibernate provides the list of built in generator classes to generate unique identifiers, all the generator classes implements the org.hibernate.id.IdentifierGenerator interface, and if your needs of unique identifiers is not solved by using built in generator classes, then you can create your own generator classes by implementing org.hibernate.id.IdentifierGenerator interface.
  • Here while saving an object into the database, the generator informs to the hibernate that, how the primary key value for the new record is going to generate.

List of built in generator classes provided by Hibernate.

1. org.hibernate.id.Assigned

The default generator class provided by Hibernate, If you explicitly not provide any generator element then, Hibernate will use assigned as a default generator strategy.
Syntax:

<id name=”generatorId” column=”generator_id”>
<generator />
</id>

OR

<id name=”generatorId” column=”generator_id”
<generator class=”assigned”/>
</id>
Note: If you are using assigned strategy then, you have to provide the identifier value of the object before save() method is called.

2. org.hibernate.id.IdentityGenerator

A generator class supported by DB2, MySql, MS Sql Server, Sybase and HypersonicSql. The returned identifiers is a type of long, int or short. The identity strategy used for auto increment columns.
Syntax:

<id name=”generatorId” column=”generator_id”>
<generator class=”identity”/>
</id>

3. org.hibernate.id.IncrementGenerator

An Identifiers that returns a long, int or short unique identifiers, calculation done by selecting the max value at startup. Not safe for use in cluster environment. Before inserting into the table it will execute an additional select query to get the max value, It is a high payee strategy think twice before opting.
Syntax:

<id name=”generatorId” column=”generator_id”>
<generator class=”increment”/>
</id>
Note:It will generate two query one for select and one for insert for example,
Hibernate: select max(generator_id) from generator
Hibernate: insert into generator (generator_name, description, generator_id) values (?, ?, ?)

4. org.hibernate.id.TableHiLoGenerator

An Identifier uses hi/low algorithm to generate unique identifier of type long, int or short. It uses a separate table for hi value. The default table uses for hi value is hibernate_unique_key with column hi_next as a hi value. Instead of default table you can create your own table and provides the details of the table and column by passing param values of generator class.
Syntax:

<id name=”generatorId” column=”generator_id”>

<generator class=”hilo”/>

</id>
OR, If you are using your own table then.

<generator class=”hilo” >
<param name=”table”>your table name</param>
<param name=”column”>your next_hi value column</param>
</generator>

5. org.hibernate.id.SequenceGenerator

An Identifier uses a Oracle style sequence and returns identifier type of long, int or short. SequenceGenerator is not supported by MySql database.
Syntax:
Create sequence in your database, example

create sequence your_seq increment by 1;
then

<generator class=”sequence”>
<param name=”sequence”>your_seq</param>
</generator>

6. org.hibernate.id.SelectGenerator

A generator class that select the just inserted row to determine the identifier value assigned by the database. The correct row is located using a unique key, must be defined unique key as natural-id.
Syntax:

<generator class=”select”>
<param name=”key”>unique_key_column</param>
</generator>

7. org.hibernate.id.GUIDGenerator

A generator class supported by MySql and MS-Sql server only, and return type is a String.
Syntax:

<id name=”generatorId” column=”generator_id”>
<generator class=”guid”/>
</id>

8. org.hibernate.id.UUIDHexGenerator

A generator class that returns a unique string of 32 length, This string will consist of only hex digits of 128 bit. This strategy is works in all databases.
Syntax:

<id name=”generatorId” column=”generator_id”>
<generator class=”uuid”/>
</id>

9. org.hibernate.id.SequenceHiLoGenerator

An Identifier that uses as a combination of hi/low algorithm with Oracle style sequence strategy, and returns identifier of type long, int or short. You can specify a maximum lo value to determine how often new hi values are fetched.
Syntax:

<generator class=”seqhilo”>
<param name=”sequence”>hi_value</param>
<param name=”max_lo”>10</param>
</generator>

10. org.hibernate.id.ForeignGenerator

A generator class that, uses the identifier of another associated object. It is usually used in conjunction with a <one to one> primary key association.
Syntax:

<generator class=”foreign”>
<param name=”property”>associated_object</param>
</generator>

11. Native

There is no any class for native generator strategy, because native has no any special behavior. It will automatically select the identifier as identity, sequence or hilo based on the capability of underlying database.
Syntax:

<generator class=”native”/>

12. Custom generator class

Yes, you can create your own generator class by implementing org.hibernate.id.IdentifierGenerator interface. Here is an example of Custom-Generator identifier.


 

   

Comments are closed