Generator classes in Hibernate

  • 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

  1. assigned
  2. increment
  3. sequence
  4. hilo
  5. native
  6. identity
  7. seqhilo
  8. uuid
  9. guid
  10. select
  11. 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.

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.

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.

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

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

6) identity

  • Identity columns are support by DB2, MYSQL, SQL SERVER  and SYBASE databases.

identity generator configuration in hibernate mapping file

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

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

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.

 

Posted in hibernate and tagged , .

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">