5.1.2.2. Identifier generator
Hibernate offers various generation strategies, let's explore the most common ones first that happens to be standardized by JPA:
-
IDENTITY: supports identity columns in DB2, MySQL, MS SQL Server, Sybase and HypersonicSQL. The returned identifier is of type
long
,short
orint
. -
SEQUENCE (called
seqhilo
in Hibernate): uses a hi/lo algorithm to efficiently generate identifiers of typelong
,short
orint
, given a named database sequence. -
TABLE (called
MultipleHiLoPerTableGenerator
in Hibernate) : uses a hi/lo algorithm to efficiently generate identifiers of typelong
,short
orint
, given a table and column as a source of hi values. The hi/lo algorithm generates identifiers that are unique only for a particular database. - AUTO: selects
IDENTITY
,SEQUENCE
orTABLE
depending upon the capabilities of the underlying database.
Important
We recommend all new projects to use the new enhanced identifier generators.
They are deactivated by default for entities using annotations but can be activated using hibernate.id.new_generator_mappings=true
.
These new generators are more efficient and closer to the JPA 2 specification semantic.
@Entity public class Customer { @Id @GeneratedValue Integer getId() { ... }; } @Entity public class Invoice { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) Integer getId() { ... }; }
These are the four standard JPA generators. Hibernate goes beyond that and provide additional generators or additional options as we will see below. You can also write your own custom identifier generator by implementing org.hibernate.id.IdentifierGenerator
.
The hbm.xml approach uses the optional <generator>
child element inside <id>
. If any parameters are required to configure or initialize the generator instance, they are passed using the <param>
element.
<id name="id" type="long" column="cat_id"> <generator class="org.hibernate.id.TableHiLoGenerator"> <param name="table">uid_table</param> <param name="column">next_hi_value_column</param> </generator> </id>
5.1.2.2.1. Various additional generators
All generators implement the interface org.hibernate.id.IdentifierGenerator
. This is a very simple interface. Some applications can choose to provide their own specialized implementations, however, Hibernate provides a range of built-in implementations. The shortcut names for the built-in generators are as follows:
-
generates identifiers of type
long
,short
orint
that are unique only when no other process is inserting data into the same table. Do not use in a cluster. -
supports identity columns in DB2, MySQL, MS SQL Server, Sybase and HypersonicSQL. The returned identifier is of type
long
,short
orint
. -
uses a sequence in DB2, PostgreSQL, Oracle, SAP DB, McKoi or a generator in Interbase. The returned identifier is of type
long
,short
orint
-
uses a hi/lo algorithm to efficiently generate identifiers of type
long
,short
orint
, given a table and column (by defaulthibernate_unique_key
andnext_hi
respectively) as a source of hi values. The hi/lo algorithm generates identifiers that are unique only for a particular database. -
uses a hi/lo algorithm to efficiently generate identifiers of type
long
,short
orint
, given a named database sequence. -
Generates a 128-bit UUID based on a custom algorithm. The value generated is represented as a string of 32 hexidecimal digits. Users can also configure it to use a separator (config parameter "separator") which separates the hexidecimal digits into 8{sep}8{sep}4{sep}8{sep}4. Note specifically that this is different than the IETF RFC 4122 representation of 8-4-4-4-12. If you need RFC 4122 compliant UUIDs, consider using "uuid2" generator discussed below.
-
Generates a IETF RFC 4122 compliant (variant 2) 128-bit UUID. The exact "version" (the RFC term) generated depends on the pluggable "generation strategy" used (see below). Capable of generating values as
java.util.UUID
,java.lang.String
or as a byte array of length 16 (byte[16]
). The "generation strategy" is defined by the interfaceorg.hibernate.id.UUIDGenerationStrategy
. The generator defines 2 configuration parameters for defining which generation strategy to use:-
Names the UUIDGenerationStrategy class to use
-
Names the UUIDGenerationStrategy instance to use
uuid_gen_strategy_class
uuid_gen_strategy
Out of the box, comes with the following strategies:
-
org.hibernate.id.uuid.StandardRandomStrategy
(the default) - generates "version 3" (aka, "random") UUID values via therandomUUID
method ofjava.util.UUID
-
org.hibernate.id.uuid.CustomVersionOneStrategy
- generates "version 1" UUID values, using IP address since mac address not available. If you need mac address to be used, consider leveraging one of the existing third party UUID generators which sniff out mac address and integrating it via theorg.hibernate.id.UUIDGenerationStrategy
contract. Two such libraries known at time of this writing include http://johannburkard.de/software/uuid/ andhttp://commons.apache.org/sandbox/id/uuid.html
-
-
uses a database-generated GUID string on MS SQL Server and MySQL.
-
selects
identity
,sequence
orhilo
depending upon the capabilities of the underlying database. -
lets the application assign an identifier to the object before
save()
is called. This is the default strategy if no<generator>
element is specified. -
retrieves a primary key, assigned by a database trigger, by selecting the row by some unique key and retrieving the primary key value.
-
uses the identifier of another associated object. It is usually used in conjunction with a
<one-to-one>
primary key association. -
a specialized sequence generation strategy that utilizes a database sequence for the actual value generation, but combines this with JDBC3 getGeneratedKeys to return the generated identifier value as part of the insert statement execution. This strategy is only supported on Oracle 10g drivers targeted for JDK 1.4. Comments on these insert statements are disabled due to a bug in the Oracle drivers.
increment
identity
sequence
hilo
seqhilo
uuid
uuid2
guid
native
assigned
select
foreign
sequence-identity
5.1.2.2.2. Hi/lo algorithm
....