- Generator classes
are used to generate the ‘identifier or primary key value for a
persistent object while saving an object in database.
- Hibernate provides
different primary key generator algorithms.
- All hibernate
generator classes implements hibernate.id.IdentifierGenerator interface,
and overrides thegenerate(SessionImplementor,Object) method to
generate the ‘identifier or primary key value‘.
- If we want our own
user defined generator, then we should implement IdentiferGenerator interface
and override the generate()
tag (which is sub element of tag) is used to configure generator class in mapping file.
Hibernate
built-in generator classes
- assigned
- increment
- sequence
- hilo
- native
- identity
- seqhilo
- uuid
- guid
- select
- foreign
1)
Assigned
- Assigned generator
class is the default generator if there is no
tag and supports in all the databases. - Developer should
assign the identifier value to entity object before saving into the
database.
2)
Increment
- Increment generator
supports in all databases and generates identifier value
for new records by using below formula.
Max
of Id value in Database + 1
- For first record it
assigns 1 to the identifier. For second record it assigns
based on above formula. i.e. (Max of Id value in Database + 1) = (1+1)
= 2.
3)
Sequence
- Sequence generator
does not support MySql database and it is database dependent. i.e. Before
using this generator, we should know whether this generator supports in
the database or not.
- If there is no sequence in database, it uses default sequence. For ex for oracle database it uses HIBERNATE_SEQUENCE
4)
hilo
- Database
independent.
- Uses hi/lo
algorithms to generate identifiers.
- It generates the
identifiers based on table and column values in
tag. - Default table
is ‘hibernate_unique_key’ and column is ‘next_hi‘.
5)
native
- It uses
internally identity or sequence or hilo generator
classes.
- native picks
up identity or sequence or hilo generator class depending
upon the capabilities of the underlying database.
6)
identity
- Identity columns are
support by DB2, MYSQL, SQL SERVER and SYBASE databases.
7)
seqhilo
- It is like hilo
generator class, but hilo generator stores its high value in table where
as seqhilo generator class stores its high value in sequence.
8)
uuid
- It uses 128-bit uuid
algorithm to generate identifiers of type string.
- Generated identifier
is unique within a network.
- Generates identifier
using IP address.
- Encodes identifier
as a string ( hexadecimal digits) of length 32.
- It is used to
generate passwords.
9)
guid
- Uses a database
generated guid string on MS-SQL and MY-SQL
10)
select
- select retrieves a
primary key assigned by a database trigger by selecting the row by some
unique key and retrieving the primary key value.
11)
Foreign
- foreign uses the
identifier of another associated object. Usually uses in conjunction with
a
primary key association.
No comments:
Post a Comment