Introduction
A sub-element of id is the class named <generator>. It is used to create a particular identification number for persistent class instances. The Hibernate Framework has a large number of generator classes.
All generator classes implement org.hibernate.id.IdentifierGenerator interface. The IdentifierGenerator interface can be implemented by the application programmer to generate custom generator classes.
Let's look at HB Generator Classes.
HB Generator Classes
Many built-in generator classes are available with the Hibernate framework. Let's have a look at some of these HB Generator classes.
- assigned
- increment
- hilo
- sequence
- native
- identity
- seqhilo
- guid
- uuid
- select
- sequence-identity
- foreign
assigned
This is the default generator approach if there is no <generator> element. The application, in this instance, assigns the id.
SYNTAX
....
<hibernate-mapping>
<class ...>
<id ...>
<generator class="assigned"></generator>
</id>
.....
</class>
</hibernate-mapping>
increment
A unique id is generated only if no other process inserts data into this table. It produces identifiers of the short, int, or long types. The application examines an identifier's maximum value if it is included in a database; otherwise, it assumes that the first created identifier is 1. Hibernate increases the identifier by 1 for each attribute value.
SYNTAX
....
<hibernate-mapping>
<class ...>
<id ...>
<generator class="increment"></generator>
</id>
.....
</class>
</hibernate-mapping>
hilo
The short, int and long type ids are generated using the high and low algorithms.
SYNTAX
.....
<id ...>
<generator class="hilo"></generator>
</id>
.....
sequence
It makes use of the database's sequence. If no sequence is defined, one is automatically created; for example, in the case of an Oracle database, a sequence named HIBERNATE SEQUENCE is created. Oracle, DB2, SAP DB, Postgre SQL, and McKoi all use sequence, but it uses a generator in Interbase.
SYNTAX
.....
<id ...>
<generator class="sequence"></generator>
</id>
.....
native
Depending on the database vendor, it employs identity, sequence, or hilo.
SYNTAX
.....
<id ...>
<generator class="native"></generator>
</id>
.....
identity
The id column is supported by it in Sybase, My SQL, MS SQL Server, DB2, and HypersonicSQL. The type of the returned id can be short, int, or long. The database is in charge of creating a unique identifier.
seqhilo
It applies the high and low algorithms to the named sequence. The type of the returned id can be short, int, or long.
guid
It makes use of a GUID created by a string-type database. Both MS SQL Server and MySQL are compatible.
uuid
To create the ID, a 128-bit UUID technique is used. The returned id is a String and is distinct across a network (because IP is used). The 32-digit hexadecimal representation of the UUID is called a hash.
select
It makes advantage of the database trigger's primary key return value.
sequence-identity
It makes use of a unique method for generating sequences. Only Oracle 10g drivers support it.
foreign
It makes use of the id of a different related item, typically <one-to-one> linkage.





