- 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 the generate(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()
- <generator /> tag (which is sub element of <id /> 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 <generator> tag and supports in all the databases.
- Developer should assign the identifier value to entity object before saving into the database.
Assigned generator configuration in hibernate mapping file.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<id name="empId" column="EMPNO"> <generator class="assigned"/> </id> OR <id name="empId" column="EMPNO"> <generator class="org.hibernate.id.Assigned"/> </id> OR <id name="empId" column="EMPNO"/> |
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.
Increment generator configuration in hibernate mapping file.
1 2 3 4 5 6 7 8 9 10 11 |
<id name="empId" column="EMPNO"> <generator class="increment"/> </id> OR <id name="empId" column="EMPNO"> <generator class="org.hibernate.id.IncrementGenerator"/> </id> |
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
Sequence generator configuration in hibernate mapping file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<id name="empId" column="EMPNO"> <generator class="sequence"> <param name="sequence">EMPNO_SEQ</param> </generator> </id> OR <id name="empId" column="EMPNO"> <generator class="org.hibernate.id.SequenceGenerator"> <param name="sequence">EMPNO_SEQ</param> </generator> </id> OR <id name="empId" column="EMPNO"> <generator class="sequence"/> </id> |
4)hilo
- Database independent.
- Uses hi/lo algorithms to generate identifiers.
- It generates the identifiers based on table and column values in <generator/> tag.
- Default table is ‘hibernate_unique_key’ and column is ‘next_hi‘.
hilo generator configuration in hibernate mapping file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<id name="empId" column="EMPNO"> <generator class="hilo"> <param name="table">HIGH_VAL_TABLE</param> <param name="column">HIGH_VAL_COLUMN</param> <param name="max_lo">60</param> </generator> </id> OR <id name="empId" column="EMPNO"> <generator class="org.hibernate.id.TableHiloGenerator"> <param name="table">HIGH_VAL_TABLE</param> <param name="column">HIGH_VAL_COLUMN</param> <param name="max_lo">60</param> </generator> </id> |
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.
native generator configuration in hibernate mapping file
1 2 3 4 5 6 7 8 9 10 11 12 |
<id name="empId" column="EMPNO"> <generator class="native"> <param name="sequnce">EMPNO_SEQ</param> <param name="table">HIGH_VAL_TABLE</param> <param name="column">HIGH_VAL_COLUMN</param> <param name="max_lo">60</param> </generator> </id> |
6) identity
- Identity columns are support by DB2, MYSQL, SQL SERVER and SYBASE databases.
identity generator configuration in hibernate mapping file
1 2 3 4 5 6 7 |
<id name="empId " column="EMPNO"> <generator class="identity"/> </id> |
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.
seqhilo configuration in hibernate mapping file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<id name="empId" column="EMPNO"> <generator class="seqhilo"> <param name="sequnce">EMPNO_SEQ</param> <param name="max_lo">60</param> </generator> </id> OR <id name="empId" column="EMPNO"> <generator class="seqhilo"> <param name="org.hibernate.id.SequenceHiLoGenerator">EMPNO_SEQ</param> <param name="max_lo">60</param> </generator> </id> |
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.
uuid configuration in hibernate mapping file
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<id name="empId" column="EMPNO"> <generator class="uuid"> </generator> </id> OR <id name="empId" column="EMPNO"> <generator class="org.hibernate.id.UUIDHexGenerator"> </generator> </id> |
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 <one-to-one> primary key association.