What are the generator class







What    is    generator  class  ?


§  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.



     <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.
<id name="empId" column="EMPNO">
    <generator class="increment"/>
 </id>
    Another way
<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 usesHIBERNATE_SEQUENCE


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
§  for the first record, the id value will be inserted as 1
§  for the second record the id value will be inserted as 32768
§  for the next records the id value will be incremented by 32768 and will stores into the database (i mean adds to the previous)
§  actually this hibernate stores the count of id values generated in a column of separated table, with name “hibernate_unique_key” by default with the column name “next_hi”
§  if we want to modify the table and column names then we need to pass 2 parameter’s for the hilo generator




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.
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.
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.
What are the generator class What    are the  generator   class Reviewed by Mukesh Jha on 2:52 AM Rating: 5

1 comment:

  1. Best Collection of Hibernate Interview Questions and Answers For Experienced are here, Happy job hunt
    hibernate interview questions

    ReplyDelete

Add your comment

All Right Reserved To Mukesh Jha.. Theme images by Jason Morrow. Powered by Blogger.