Hibernate generator element generates the primary key for new record. There are provided options provided by the generator method to be used in different situations.
The <generator> element
Here is the example
<Generator class = "assigned"/>
Here are the list of some commonly used generators in hibernate:
Generator |
Description |
Increment |
It generates identifiers of type long, short or int that are unique only when no other process is inserting data into the same table. It shocould not the used in the clustered environment. |
Identity |
It supports identity columns in DB2, MySQL, ms SQL Server, Sybase and hypersonicsql. The returned identifier is of type long, short or Int. |
Sequence |
The sequence generator uses a sequence in DB2, PostgreSQL, Oracle, sap db, mckoi or a generator in InterBase. The returned identifier is of type long, short or int |
HiLo |
The HiLo generator uses a hi/LO algorithm to efficiently generate identifiers of type long, short or Int, given a table and column (by default hibernate_unique_key and next_hi respectively) as a source of HI values. the HI/LO algorithm generates identifiers that are unique only for a particle database. do not use this generator with connections enlisted with JTA or with a user-supplied connection. |
Seqhilo |
The seqhilo generator uses a hi/LO algorithm to efficiently generate identifiers of type long, short or Int, given a named database sequence. |
UUID |
The UUID generate uses a 128-bit UUID algorithm to generate identifiers of Type string, unique within a network (the IP address is used ). the UUID is encoded as a string of hexadecimal digits of Length 32. |
Guid |
It uses a database-generated guid string on ms SQL Server and MySQL. |
Native |
It picks identity, sequence or HiLo depending upon the capabilities of the underlying database. |
Assigned |
Lets the application to assign an identifier to the object before save () is called. This is the default strategy if no <generator> element is specified. |
Select |
Retrieves a primary key assigned by a database trigger by selecting the row by some unique key and retrieving the primary key value. |
Foreign |
Uses the identifier of another associated object. usually used in conjunction with a <one-to-one> Primary Key Association. |